@EnableAspectJAutoProxy does not work

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

I am using Spring Boot, and I would like to use AspectJ with it.

The following works (of course):

 @Aspect @Component public class RequestMappingAspect {      @Before("@annotation(org.springframework.web.bind.annotation.RequestMapping)")     public void advice(JoinPoint joinPoint) {         ...     } } 

However, if @Component is removed and @EnableAspectJAutoProxy is added, the following does not work.

 @SpringBootApplication @EnableSwagger2 @EnableAspectJAutoProxy public class Application {      public static void main(String[] args) {         SpringApplication.run(Application.class, args);     } } 

How to enable AspectJ auto proxy correctly?

回答1:

Wondering about the same thing, we ended up doing something similar to this:

@EnableAspectJAutoProxy(proxyTargetClass = true) @Configuration("Main applicationContext") @ComponentScan(     basePackages = {"com.where.ever"},     excludeFilters = {@ComponentScan.Filter(Aspect.class)}) public class ApplicationConfiguration {     @Bean(autowire = Autowire.BY_TYPE)     public SomeAspect someAspect() {         return Aspects.aspectOf(SomeAspect.class);     }     ...     ... } 

This enabled us to just add the @Aspect-annotation on the aspects, which also wired them correctly. Might be that this was a pointless reply, however, it explains how we solved the issue - and not the actual solution to the problem. Let me know if you want this to be deleted.



回答2:

You need both @EnableAspectJAutoProxy for the spring configuration and combination of @Aspect / @Component annotations

@EnableAspectJAutoProxy does the same thing as xml based <aop:aspectj-autoproxy>



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!