Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 377
user1374907 Avatar asked May 04 '12 12:05

user1374907


People also ask

How do I enable AspectJ annotations support in Spring AOP?

To enable @AspectJ, spring AOP provides @EnableAspectJAutoProxy annotation which will be annotated in java configuration. To work with spring AOP and @AspectJ support, we need to create a class annotated with @Aspect annotation.

What does AOP AspectJ-Autoproxy /> do?

@AspectJAutoProxy. Fashioned after Spring XML's <aop:aspectj-autoproxy> , @AspectJAutoProxy detects any @Aspect beans and generates proxies as appropriate to weave the advice methods in those aspects against other beans in the container.

Which tag informs the Spring container about the use of AspectJ annotation?

Which tag informs the spring container about the use of AspectJ annotation? Explanation: To enable AspectJ annotation support in the Spring IoC container, you only have to define an empty XML element aop:aspectj-autoproxy in your bean configuration file.


1 Answers

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();      }  } 
like image 73
Sean Patrick Floyd Avatar answered Sep 17 '22 14:09

Sean Patrick Floyd