@EnableAspectJAutoProxy does not work

Dante May Code picture Dante May Code · Jan 23, 2017 · Viewed 10.4k times · Source

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?

Answer

Kalpesh Soni picture Kalpesh Soni · Feb 10, 2017

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>