2011-12-12 37 views
6

我想設置沒有任何XML的Spring AOP,並想知道如何以這種方式啓用自動代理。沒有XML的Spring AOP

定義一個AutoProxyCreator-bean的工作,但沒有一個更簡單的方法嗎?

這是我@Configuration是什麼樣子:

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

所有其他豆類由AnnotationConfigApplicationContext掃描英寸

回答

7

Spring 3.0.x不提供簡單的方法來替換@Configuration中的XML名稱空間擴展(例如<aop:aspectj-autoproxy>)。

即將到來的Spring 3.1將支持用於此目的的特殊註釋,例如@EnableAspectJAutoProxy

+0

謝謝你,所以我想我的解決方案是確定暫且;) – CKuck

0

最後,我發現了一個美觀的方式來添加AnnotationAwareAspectJAutoProxyCreator

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