2012-05-04 2 views
33

XMLなしでSpring AOPを設定しようとしています。 に@Configurationと注釈を付けたクラスで<aop:aspectj-autoproxy>を有効にしたいと思います。Javaベースのアノテーションで<aop:aspectj-autoproxy>を有効にする方法

これは、それがXMLファイルで定義されるような方法である:

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

私は@Configuration@EnableAspectJAutoProxy で私のクラスに注釈を付けることを試みたが、何も起こりませんでした。

+0

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/EnableAspectJAutoProxy.html – firstpostcommenter

答えて

42

同じ@ConfigurationクラスのアスペクトBeanを作成しましたか?

@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(); 
    } 
} 
+1

はい、私が作成した側面を: ここthe docsが示唆するものです例のように同じ@ConfigurationクラスにBeanを追加します。しかし、何も起こりません。余分なxmlファイルに をインポートし、これをImportResource経由でConfigurationクラスに追加しました。今、アスペクトが動作します。しかし、より良い方法は、同じコンフィグレーションクラスのコンフィグレーションを持つことです – user1374907

+0

まさに私が探しているものです。クラス名でさえ私のものと同じです:D –

+0

私は私のAspectのための@ Beanを宣言しませんが、アドバイスは動作します、なぜですか? – Jaskey

関連する問題