美文网首页
Aop:advice

Aop:advice

作者: 李拾叁的摸鱼日常 | 来源:发表于2019-10-26 15:11 被阅读0次

Tips

  • 配置类使用@EnableAspectJAutoProxy
  • @Pointcut通过方法定义切点表达式,引用切点表达式时当前类/非当前类差别
  • JoinPoint作为方法参数时要在参数列表首位
  • @Around一定要执行目标方法

@Aspect
public class AspectComponent {

    @Pointcut("execution(public * com.foo.service.*.*(..))")
    public void pointCut() {

    }

    /**
     * 当前类
     */
    @Before("pointCut()")
    public void before() {
        System.out.println("before");
    }

    /**
     * 外部类
     */
    @Before("com.foo.component.AspectComponent.pointCut()")
    public void after() {
        System.out.println("after");
    }

    /**
     *
     * @param joinPoint 必须放在方法的first place
     * @param result 参数与注解中的名称做映射
     */
    @AfterReturning(value = "pointCut()", returning = "result")
    public void afterReture(JoinPoint joinPoint, int result) {
        System.out.println("afterReture targetMeth:" + joinPoint.getSignature().getName() + "  returnValue:" + result);
    }

    @AfterThrowing(value = "pointCut()", throwing = "excepion")
    public void afterThrowing(JoinPoint joinPoint, Exception excepion) {
        System.out.println("afterThrowing targetMeth:" + joinPoint.getSignature().getName() + "  excepton:" + excepion);
    }

    @Around(value="pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("before around ");
        System.out.println("around "+pjp.getTarget().getClass());
        Object[] args = pjp.getArgs();
        System.out.println("around "+args[0]);
        Object result = pjp.proceed(args);
        System.out.println("result="+result);
        System.out.println("after around ");
        return result;
        /*
        @Around 一定要有返回值,不然报错,后续advice没法执行
         */
    }
}

相关文章

  • Spring接口代理实现

    AOP联盟通知类型AOP联盟为通知Advice定义了org.aopalliance.aop.Advice 开发通知...

  • Aop:advice

    Tips 配置类使用@EnableAspectJAutoProxy @Pointcut通过方法定义切点表达式,引用...

  • spring aop使用

    概念 Interceptor VS AdviceInterceptor是Advice中的一种Advice是AOP编...

  • Spring AOP 代理

    Spring AOP 代理 1. Spring AOP 增强类型 AOP 联盟为通知 Advice 定义了 org...

  • 笔试题 计算机

    spring aop通知(advice)分成五类:前置通知[Before advice]:在连接点前面执行,前置通...

  • Spring一般切面

    Spring一般切面 Spring 实现了AOP联盟Advice这个接口=>org.aopalliance.aop...

  • Spring AOP

    Spring AOP 一、面向切面编程 1. AOP术语 1.1 ADVICE(增强) Spring提供了以下几种...

  • 03 AOP学习之五种通知

    Spring AOP五种通知详解 spring aop通知(advice)分成五类: 前置通知Before adv...

  • Sping - AOP - Advice

    原文地址:https://mkyong.com/spring/spring-aop-examples-advice...

  • Spring in Action读书笔记

    Bean @Scope AOP术语 通知 (Advice) 连接点(Join point) 切点(Poincut)...

网友评论

      本文标题:Aop:advice

      本文链接:https://www.haomeiwen.com/subject/cfzyvctx.html