6
votes

I am trying to set up Spring AOP without any XML and wonder how to enable auto-proxying this way.

Defining an AutoProxyCreator-bean works, but isn't there an easier way?

This is what my @Configuration looks like:

@Configuration
public class Context {
    @Bean
    public AnnotationAwareAspectJAutoProxyCreator annotationAwareAspectJAutoProxyCreator() {
        return new AnnotationAwareAspectJAutoProxyCreator();
    };
    ...
}

All other beans are scanned in by AnnotationConfigApplicationContext.

2

2 Answers

7
votes

Spring 3.0.x doesn't provide easy ways to replace XML namespace extensions (such as <aop:aspectj-autoproxy>) in @Configuration.

An upcoming Spring 3.1 will support special annotations for this purpose, such as @EnableAspectJAutoProxy.

0
votes

Finally I found an aesthetically pleasing way to add the AnnotationAwareAspectJAutoProxyCreator:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AnnotationAwareAspectJAutoProxyCreator.class);
context.scan("com.myDomain");
context.refresh();