@EnableAspectJAutoProxy not working

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 @Component 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 proxy server correctly?

+5
source share
2 answers

Thinking of the same thing, we ended up with something similar:

 @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 allowed us to simply add @Aspect -nnotation to aspects that also connected them correctly. This may have been a meaningless answer, but it does explain how we solved the problem, not the actual solution to the problem. Let me know if you want this to be deleted.

+1
source

For spring configuration and a combination of @ Aspect / @ Component annotations you need @EnableAspectJAutoProxy as well

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

+2
source

Source: https://habr.com/ru/post/1263195/


All Articles