Springboot注解@Aspect(二)JoinPoint 使用详解

目录

JoinPoint 的作用

JoinPoint 常用方法

示例

JoinPoint 的子类和关联类


JoinPoint 的作用

        在 Spring AOP 中,JoinPoint 接口代表了一个程序执行的点,比如方法执行或异常处理。当使用 AOP 通知(Advice)时,你可以将 JoinPoint 作为参数传递到通知方法中,以便获取有关当前执行点的详细信息。

  JoinPoint 提供了一种方式来访问当前被通知方法的详细信息,如方法签名、参数等。这在编写通知逻辑时非常有用,因为你可以根据当前执行的方法来修改通知的行为。

    JoinPoint

  • 用于所有类型的通知(@Before@After@AfterReturning@AfterThrowing),但不包括环绕通知。

JoinPoint 常用方法

  1. getArgs():返回一个对象数组,包含了被通知方法的参数。

  2. getThis():返回代理对象。

  3. getTarget():返回目标对象。

  4. getSignature():返回被通知方法的签名信息。

  5. toString():打印出正在执行的被通知方法的详细信息。

  6. toShortString():提供正在执行的被通知方法的简短描述。

  7. toLongString():提供正在执行的被通知方法的完整描述

示例

假设你有一个前置通知(Before),你想在方法执行之前打印方法名称和参数:

ProceedingJoinPoint对象:ProceedingJoinPoint对象是JoinPoint的子接口,该对象只用在@Around的切面方法中。

@Aspect
@Component
public class aopAspect {
    /**
     * 定义一个切入点表达式,用来确定哪些类需要代理
     * execution(* aopdemo.*.*(..))代表aopdemo包下所有类的所有方法都会被代理
     */
    @Pointcut("execution(* aopdemo.*.*(..))")
    public void declareJoinPointerExpression() {}
 
    /**
     * 前置方法,在目标方法执行前执行
     * @param joinPoint 封装了代理方法信息的对象,若用不到则可以忽略不写
     */
    @Before("declareJoinPointerExpression()")
    public void beforeMethod(JoinPoint joinPoint){
        System.out.println("目标方法名为:" + joinPoint.getSignature().getName());
        System.out.println("目标方法所属类的简单类名:" +        joinPoint.getSignature().getDeclaringType().getSimpleName());
        System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
        //获取传入目标方法的参数
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("第" + (i+1) + "个参数为:" + args[i]);
        }
        System.out.println("被代理的对象:" + joinPoint.getTarget());
        System.out.println("代理对象自己:" + joinPoint.getThis());
    }
 
    /**
     * 环绕方法,可自定义目标方法执行的时机
     * @param pjd JoinPoint的子接口,添加了
     *            Object proceed() throws Throwable 执行目标方法
     *            Object proceed(Object[] var1) throws Throwable 传入的新的参数去执行目标方法
     *            两个方法
     * @return 此方法需要返回值,返回值视为目标方法的返回值
     */
    @Around("declareJoinPointerExpression()")
    public Object aroundMethod(ProceedingJoinPoint pjd){
        Object result = null;
 
        try {
            //前置通知
            System.out.println("目标方法执行前...");
            //执行目标方法
            //result = pjd.proeed();
            //用新的参数值执行目标方法
            result = pjd.proceed(new Object[]{"newSpring","newAop"});
            //返回通知
            System.out.println("目标方法返回结果后...");
        } catch (Throwable e) {
            //异常通知
            System.out.println("执行目标方法异常后...");
            throw new RuntimeException(e);
        }
        //后置通知
        System.out.println("目标方法执行后...");
 
        return result;
    }
}

被代理类

/**
 * 被代理对象
 */
@Component
public class TargetClass {
    /**
     * 拼接两个字符串
     */
    public String joint(String str1, String str2) {
        return str1 + "+" + str2;
    }
}

测试类

public class TestAop {
    @Test
    public void testAOP() {
        //1、创建Spring的IOC的容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:bean.xml");
 
        //2、从IOC容器中获取bean的实例
        TargetClass targetClass = (TargetClass) ctx.getBean("targetClass");
 
        //3、使用bean
        String result = targetClass.joint("spring","aop");
        System.out.println("result:" + result);
    }
}

结果:

目标方法执行前...
目标方法名为:joint
目标方法所属类的简单类名:TargetClass
目标方法所属类的类名:aopdemo.TargetClass
目标方法声明类型:public
第1个参数为:newSpring
第2个参数为:newAop
被代理的对象:aopdemo.TargetClass@4efc180e
代理对象自己:aopdemo.TargetClass@4efc180e
目标方法返回结果后...
目标方法执行后...
result:newSpring+newAop

JoinPoint 的子类和关联类

  1. MethodSignature

    • MethodSignatureSignature 接口的子接口,专门用于方法调用。它提供了访问被拦截方法的详细信息,如方法名称、返回类型和参数类型。
    • 在通知方法中,通常通过将 JoinPoint.getSignature() 的返回值强制转换为 MethodSignature 来获取更多关于方法的信息。
  2. ProceedingJoinPoint

    • ProceedingJoinPointJoinPoint 的子接口,专门用于环绕通知(@Around。它添加了 proceed() 方法,允许控制何时继续执行拦截的方法。
    • proceed() 方法是环绕通知中的关键,它决定了是否继续执行原方法或者提前返回自定义结果。

------------------------------------------与正文内容无关------------------------------------
如果觉的文章写对各位读者老爷们有帮助的话,麻烦点赞加关注呗!作者在这拜谢了!

混口饭吃了!如果你需要Java 、Python毕设、商务合作、技术交流、就业指导、技术支持度过试用期。请在关注私信我,本人看到一定马上回复!

  • 13
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
@Aspect注解Spring框架中用于定义切面的注解。通过在一个类上添加@Aspect注解,该类就成为了一个切面。在该类中,我们可以定义各种各样的通知(Advice),如@Before、@After、@Around等等,来拦截、增强目标方法的执行。 @Aspect注解需要和其他注解配合使用,其中最常用的注解是@Pointcut和@Before/@After/@Around等通知注解。@Pointcut注解用于定义切点,即需要被拦截的目标方法,而@Before/@After/@Around等通知注解则用于定义具体的拦截逻辑。 例如,我们可以在一个类中定义如下的@Aspect切面: ```java @Aspect @Component public class LogAspect { @Pointcut("execution(* com.example.demo.service..*.*(..))") public void serviceMethod() {} @Before("serviceMethod()") public void before(JoinPoint joinPoint) { // 在目标方法执行之前执行的逻辑 ... } @AfterReturning("serviceMethod()") public void afterReturning(JoinPoint joinPoint) { // 在目标方法执行之后执行的逻辑 ... } @AfterThrowing("serviceMethod()") public void afterThrowing(JoinPoint joinPoint) { // 在目标方法抛出异常时执行的逻辑 ... } @Around("serviceMethod()") public Object around(ProceedingJoinPoint pjp) throws Throwable { // 在目标方法执行前后执行的逻辑 ... Object result = pjp.proceed(); ... return result; } } ``` 在上述代码中,我们使用@Pointcut注解定义了一个切点serviceMethod(),该切点匹配所有com.example.demo.service包及其子包中的所有方法。然后我们使用@Before、@AfterReturning、@AfterThrowing和@Around注解定义了各种通知,来实现在目标方法执行前后、抛出异常时执行相关逻辑的功能。最后,我们将该类标注为@Component,使得Spring容器可以自动扫描并加载该切面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A乐神

恭喜发财啊,老板,嘻嘻!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值