4

以 Annotation 执行顺序来探究 AOP

 1 year ago
source link: https://www.jansora.com/notebook/107550
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

以 Annotation 执行顺序来探究 AOP闲文杂记以 Annotation 执行顺序来探究 AOP 导航前往主站应用闲文杂记代码在线代码魔方房价浅析

/**
 * <Description> Description for A <br>
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface A {

}



/**
 * <Description> Description for B <br>
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface B {

}



/**
 * <Description> Description for 注解顺序 <br>
 */
@Component
public class AnnotationOrderDemo extends AbstractDemoFactory {

    @A
    @B
    public void testABeforeB() {

    }

    @B
    @A
    public void testAAfterB() {

    }
}



@Component
@Aspect
public class AspectA {

    /**
     * logger
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(AspectA.class);

    @Around("@annotation(com.jansora.demo.annotation.lib.annotation.A)")
    public Object a(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        LOGGER.debug("aspect a.");
        return proceedingJoinPoint.proceed();
    }


    @Around("@annotation(com.jansora.demo.annotation.lib.annotation.B)")
    public Object b(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        LOGGER.debug("aspect b.");
        return proceedingJoinPoint.proceed();
    }

}




@SpringBootApplication
public class AnnotationApplication implements ApplicationRunner {

    @Autowired
    AnnotationOrderDemo annotationOrderDemo;

    private String[] args;

    public static void main(String[] args) throws Throwable {
        SpringApplication.run(AnnotationApplication.class, args);
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        this.args = args.getSourceArgs();
        System.out.println("testABeforeB: ");
        annotationOrderDemo.testABeforeB();
        System.out.println("testAAfterB: ");
        annotationOrderDemo.testAAfterB();
    }
}

默认情况(不作任何干预)

2022-08-04 11:03:11.429  INFO 32664 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 19932 (http) with context path ''
2022-08-04 11:03:11.443  INFO 32664 --- [  restartedMain] c.j.d.annotation.AnnotationApplication   : Started AnnotationApplication in 2.217 seconds (JVM running for 2.65)
testABeforeB: 
2022-08-04 11:03:11.449 DEBUG 32664 --- [  restartedMain] c.j.demo.annotation.lib.aspect.AspectA   : aspect a.
2022-08-04 11:03:11.450 DEBUG 32664 --- [  restartedMain] c.j.demo.annotation.lib.aspect.AspectA   : aspect b.
testAAfterB: 
2022-08-04 11:03:11.454 DEBUG 32664 --- [  restartedMain] c.j.demo.annotation.lib.aspect.AspectA   : aspect a.
2022-08-04 11:03:11.454 DEBUG 32664 --- [  restartedMain] c.j.demo.annotation.lib.aspect.AspectA   : aspect b.

结论: 不管 A 在 B 前, 还是 A 在 B 后, 默认情况下显示, A 的执行顺序都在 B 前.

那么问题来了

问题 1: 为什么两次执行结果不一致?

问题 2: 为什么两次执行结果都是 A 在 B 前, 而不是 B 在 A 前?

带着这两个问题, 我们来 DEBUG 一下

VulNj6.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK