2013-02-05 5 views
7

私はカスタム注釈付きのタグ付けされたメソッドをインターセプトしようとしていますが、これを読んだ理由は動作させることができないためです。私は単純な例に従ってきましたが、それを動作させることはできません。Spring 3.2 AOP - 注釈によるメソッドの傍受

ここに私のコードです。

MyAnnotation.java:

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface MyAnnotation { 
    String value() default ""; 
    String key() default ""; 
    String condition() default ""; 
} 

MyAspect.java:

@Aspect 
public class MyAspect { 

    @Pointcut(value="execution(public * *(..))") 
    public void anyPublicMethod() { } 

    @Around("anyPublicMethod() && @annotation(myAnnotation)") 
    public Object process(ProceedingJoinPoint jointPoint, MyAnnotation myAnnotation) throws Throwable { 
    System.out.println("In AOP process"); 
    return 5; //jointPoint.proceed(); 
    } 
} 

スプリング-config.xmlの:

<beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:mvc="http://www.springframework.org/schema/mvc" 
      xmlns:c="http://www.springframework.org/schema/c" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xmlns:tx="http://www.springframework.org/schema/tx" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:aop="http://www.springframework.org/schema/aop" 
      xmlns:p="http://www.springframework.org/schema/p" 
      xmlns:cache="http://www.springframework.org/schema/cache" 
      xmlns:task="http://www.springframework.org/schema/task" 
      xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.2.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
      http://www.springframework.org/schema/cache 
      http://www.springframework.org/schema/cache/spring-cache-3.2.xsd 
      http://www.springframework.org/schema/task 
      http://www.springframework.org/schema/task/spring-task-3.2.xsd"> 


    ... 

    <context:component-scan base-package="com.myapp"> 
    <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/> 
    </context:component-scan> 


    <aop:aspectj-autoproxy proxy-target-class="true" /> 
    <bean id="myAspect" class="com.myapp.annotation.MyAspect" /> 

    ... 

MyComponent.java:

@Component 
public class MyComponent { 
    @MyAnnotation(value="valtest", key="keytest", condition="contest") 
    public int add(int i, int j) { 
    System.out.println("Executing annotation.add"); 
    return i+j; 
    } 
} 

テストコード:

final MyComponent m = new MyComponent(); 
assertTrue(5 == m.add(0, 1)); // Here m.add(...) always returns 1 instead of 5. 

私はすべてanyPublic(を使用してとせずに、私のポイントカットさまざまな方法を定義しようとした側の注意点として)方法とその実行ポイントカットが、それらのどれも私のために働いていない:

  1. @Around("@annotation(com.myapp.annotation.MyAnnotation)") public Object process(ProceedingJoinPoint jointPoint) throws Throwable { .. }

  2. @Around(value="@annotation(myAnnotation)", argNames="myAnnotation") public Object process(ProceedingJoinPoint jointPoint, MyAnnotation myAnnotation) throws Throwable { .. }

  3. @Around("execution(* com.myapp.*.*(..)) && @annotation(com.myapp.annotation.MyAnnotation)") public Object process(ProceedingJoinPoint jointPoint) throws Throwable { .. }

私が間違って何をしているのですか?

+1

アプリケーションコンテキストを通過する代わりに 'new'を介してインスタンス化を許可するようにバイトコードを具体化していないか、コンパイル時にウィービングする場合を除いて、' new'を呼び出しています。インスタンス化されたオブジェクトは、またはAOPに関連する。 –

+0

ありがとうございましたDaveそれでした!私は再びそれのために落ちたと信じられない。時には明白なことを強調するためにもう一組の目が必要になることもあります。 – Lancelot

+0

この質問/回答はちょうどトンを手伝った!また、@ annotation(myAnnotation)の 'my​​Annotation'は、プロセス宣言のパラメータ名と一致するように、小文字で始まります。私たちはそれに気づく前に、 "仮引数が指定されていない"ことを見ていました。 –

答えて

8

テストコードでは、MyComponentを作成することをSpringに許可するのではなく、代わりにnew演算子を使用しています。春のApplicationContextからMyComponentにアクセスする必要があります。

public class SomeTest { 
    public static void main(String[] args) throws Exception { 
     final ApplicationContext appContext = new ClassPathXmlApplicationContext("spring-config.xml"); 
     final MyComponent myComponent = appContext.getBean(MyComponent.class); 
     //your test here... 
    } 
} 

コンポーネントをSpringから取得しない場合、どのようにプロキシされると思われますか?

関連する問題