39

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.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
user1374907
  • 391
  • 1
  • 3
  • 3

2 Answers2

48

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();
     }
 }
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 1
    Yes, I created an aspect bean in the same @Configuration class as shown in your example. But still nothing happens. I now imported the in an extra xml-file and added this via ImportResource to my Configuration class. Now the Aspect works. But the better way would be to have the configuration in the same Configuration class – user1374907 May 07 '12 at 15:48
  • 1
    Exactly what I looking for. Even the class name is the same with mine :D – Thai Tran Mar 27 '14 at 04:14
  • I don't declare a @Bean for my Aspect , but the advice works, why? – JaskeyLam Nov 01 '14 at 14:15
  • 1
    @Jaskey picked up by component scan? – Sean Patrick Floyd Nov 02 '14 at 16:16
  • @SeanPatrickFloyd, yes, I use component scan in my xxx-servet.xml – JaskeyLam Nov 04 '14 at 04:54
  • @Jaskey hmmm, I can only explain that if your aspect is also annotated as `@Component`or similar. See [the relevant section of the manual](http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#aop-at-aspectj) – Sean Patrick Floyd Nov 04 '14 at 07:56
  • So, can I think that @component or @Bean-->return an instance is doing the same thing in Spring, which is trying to declare a bean for Spring container to hold? – JaskeyLam Nov 04 '14 at 09:12
  • yeah, pretty much. they are two different ways to achieve the same thing, make a class instantiated and managed by Spring – Sean Patrick Floyd Nov 04 '14 at 09:51
  • Thanks Sean. its very useful. – Lova Chittumuri Sep 21 '17 at 11:50
  • 1
    remember to add proxyTargetClass = true otherwise you can have unexpected errors if you used in controller @EnableAspectJAutoProxy(proxyTargetClass = true) – FarukT Jun 05 '19 at 10:10
  • 1
    @FarukT that's for when you use class-based proxies. I prefer to use interface based proxies. – Sean Patrick Floyd Jun 07 '19 at 02:03
3

I used the accepted answer solution but I had unexpected problems and never understand untill to add this parameter to configuration.

@EnableAspectJAutoProxy(proxyTargetClass = true)

If you use annotation into @Controller you'll need to configure in this way

remember if you have java 8 you need to use a version of AspectJ greater than 1.8.X

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AppConfig {

    @Bean
    public AccessLoggerAspect accessLoggerAspect() {
        return new AccessLoggerAspect();
    }

}
FarukT
  • 1,560
  • 11
  • 25