2012-05-02 7 views
0

私は、ばねに新しいですを使用してJavaのアドバイスを持つ単純なJavaアプリケーションを実行しようとしている春のアドバイス(@Before)を使用することができません.......</p> <p>xmlファイルを注釈

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
    <aop:aspectj-autoproxy> 
     <aop:include name="com.cts.two.Advices"/> 
    </aop:aspectj-autoproxy>  
    <context:annotation-config/> 
    <context:component-scan base-package="com.cts.two"></context:component-scan> 
</beans> 

アドバイスアドバイスを適用する必要のあるクラス

package com.cts.two; 

import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.Pointcut; 
@Aspect 
public class Advices implements Adv{  

    @Pointcut("execution(* com.cts.two.*.*(..))") 
    public void advice(){ 

    } 
    @Before("advice()") 
    public void before(JoinPoint name) throws Throwable{ 
     System.out.println("inside advices"); 
     /*System.out.println(name.getClass() + " this is get class"); 
     System.out.println(name.getSignature().getName() + " this is the get signatue and get name");*/ 
    } 
} 

クラスが...私はアドバイスクラスのメソッドの前に下記の試験()メソッド

前に実行させたいですメソッドの
package com.cts.two; 

import org.springframework.stereotype.Component; 
@Component 

public class ClassA { 
    private ClassB b= new ClassB(); 

    public void setB(ClassB b) { 
     this.b = b; 
    } 
    public void test(){ 
     System.out.println("inside classA test"); 
     //b.test(); 
    } 

} 

、発信者/テストクラス/メインクラス

package com.cts.two; 

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class CallerAB { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     ApplicationContext context = new ClassPathXmlApplicationContext(
       "AllAnnotations.xml"); 
     ClassA calledA = (ClassA) context.getBean("classA"); 
     calledA.test(); 
    } 

} 

問題は、私は、コードを実行すると、直接クラスAの試験方法が実行されていることですが、アドバイスはありません... 親切ですアドバイス.. 何か不足していますか? AspectJ 1.6.12 jarも追加されました...

答えて

2

アスペクトはBeanとしてデカールする必要があります。

@Aspectは自動的に行いませんが、<aop:include>も同様です(アスペクトとして使用できるBeanには追加の制限が設定されています)。

だから、あなたは

@Aspect 
@Component 
public class Advices implements Adv { ... } 

を必要と<aop:include>を必要としません。

+0

申し訳ありませんが私はあなたが私にしたい変更をunderstndしないでください? –

+0

''で表示するようにするには、 '@ Component'でアスペクトクラスに注釈を付けるか、XMLに' 'と宣言してください。 – axtavt

+0

ええ、それはやったけど.....まだアドバイスが呼ばれていない...直接クラスのテストメソッドは.......と呼ばれています –

0

@axtavtの回答に記載されているように、@Componentアノテーションを追加する必要があります。しかし、<aop:include>も削除する必要があります。あなたの春の配線XMLだけで次のようになります。spring AOP documentationに述べたように

<aop:aspectj-autoproxy/> 
<context:annotation-config/> 
<context:component-scan base-package="com.cts.two"/> 

<aop:include>要素でname属性は、Bean名ではなくクラス名であることを想定しています。 Beanを明示的に指定すると、Springの自動検出がオーバーライドされ、間違って指定すると、使用されるアスペクトがまったくないことを意味します。

関連する問題