Aspectj的JointPoint对象详解

本文介绍了AOP中JointPoint的重要方法,包括获取切入点参数、获取目标对象信息及连接点类型的getArgs、getSignature与getKind方法,并提供了具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JointPoint有很多方法,下面将介绍其常用的一些方法。

getArgs

获取切入点的参数。示范如下:

    @Before("myPointCut()")
    public void myBefore(JoinPoint joinPoint) {
        Object[] objects = joinPoint.getArgs();
        System.out.println((String)objects[0]);
        System.out.println((Integer)objects[1]);
    }

切入点

public void check(String thing, int time) {
        System.out.println(thing + "检查了" + time + "次");
    }
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/testAspectj/applicationContext.xml");
        CheckBean checkBean = (CheckBean) applicationContext.getBean("checkBean");
        checkBean.check("帐户名", 3);
    }

后台输出结果如下

帐户名
3
帐户名检查了3次

getSignature

返回包含目标对象信息的Signature对象,你可以通过Signature对象获得目标对象的Class对象,以及名字,修饰符等等,实例代码如下

    @Before("myPointCut()")
    public void myBefore(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println(signature.getDeclaringType());
        System.out.println(Modifier.toString(signature.getModifiers()));
        System.out.println(signature.getDeclaringTypeName());
    }
public class CheckBean {
    public void check(String thing, int time) {
        System.out.println(thing + "检查了" + time + "次");
    }
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/testAspectj/applicationContext.xml");
        CheckBean checkBean = (CheckBean) applicationContext.getBean("checkBean");
        checkBean.check("帐户名", 3);
    }
}

运行结果如下

class testAspectj.CheckBean
public
testAspectj.CheckBean
帐户名检查了3

getKind

返回连接点的类型。实例代码如下

    @Before("myPointCut()")
    public void myBefore(JoinPoint joinPoint) {
        System.out.println(joinPoint.getKind());
    }
public class CheckBean {
    public void check(String thing, int time) {
        System.out.println(thing + "检查了" + time + "次");
    }
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/testAspectj/applicationContext.xml");
        CheckBean checkBean = (CheckBean) applicationContext.getBean("checkBean");
        checkBean.check("帐户名", 3);
    }
}

运行结果如下

method-execution
帐户名检查了3次
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值