How to enable <aop:aspectj-autoproxy> with java-based annotations

user1374907 picture user1374907 · May 4, 2012 · Viewed 31.2k times · Source

I am trying to set up Spring AOP without any XML. I'd like to enable <aop:aspectj-autoproxy> in a class which is annotated with @Configuration.

This is the way it would be defined in an XML-file:

<aop:aspectj-autoproxy>
<aop:include name="msgHandlingAspect" />
</aop:aspectj-autoproxy>

I tried to annotate my class with @Configuration and @EnableAspectJAutoProxy but nothing happened.

Answer

Sean Patrick Floyd picture Sean Patrick Floyd · May 4, 2012

Did you create an aspect bean in the same @Configuration class? Here's what the docs suggest:

 @Configuration
 @EnableAspectJAutoProxy
 public class AppConfig {
     @Bean
     public FooService fooService() {
         return new FooService();
     }

     @Bean // the Aspect itself must also be a Bean
     public MyAspect myAspect() {
         return new MyAspect();
     }
 }