Spring AOP works without @EnableAspectJAutoProxy?

后端 未结 1 1885
礼貌的吻别
礼貌的吻别 2021-01-07 20:31

I am learning Spring (currently its AOP framework). Even though all sources I\'ve read say that to enable AOP one needs to use @EnableAspectJAutoProxy annotatio

相关标签:
1条回答
  • 2021-01-07 20:48

    The @SpringBootApplication annotation contains the @EnableAutoConfiguration annotation. This autoconfiguration is one of the attractions of Spring Boot and makes configuration simpler. The auto configuration uses @Conditional type annotations (like @ConditionalOnClass and @ConditionalOnProperty) to scan the classpath and look for key classes that trigger the loading of 'modules' like AOP.

    Here is an example AopAutoConfiguration.java

    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.reflect.Advice;
    import org.aspectj.weaver.AnnotatedElement;
    
    @Configuration
    @ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class,
        AnnotatedElement.class })
    @ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
    public class AopAutoConfiguration {
    
      @Configuration
      @EnableAspectJAutoProxy(proxyTargetClass = false)
      @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)
      public static class JdkDynamicAutoProxyConfiguration {
    
      }
    
      @Configuration
      @EnableAspectJAutoProxy(proxyTargetClass = true)
      @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
      public static class CglibAutoProxyConfiguration {
    
      }
    
    }
    

    As you can see, if you add one of the above aop classes to your class path (or property), Spring will detect it and effectively behave as if you had the @EnableAspectJAutoProxy annotation on your main class.

    Your project has a file LoggerHogger which has an @Aspect.

    0 讨论(0)
提交回复
热议问题