2012-05-11 16 views
1

特定のアノテーションでマークされたクラスに属するすべてのパブリックメソッドをターゲットにするアスペクトを作成するにはどうすればよいですか? method1()method2()はaspectで処理し、method3()はaspectで処理しないでください。SpringとAspectJを使用したクラスでのアスペクトベースのアノテーションのターゲティング

@SomeAnnotation(SomeParam.class) 
public class FooServiceImpl extends FooService { 
    public void method1() { ... } 
    public void method2() { ... } 
} 

public class BarServiceImpl extends BarService { 
    public void method3() { ... } 
} 

注釈をメソッドレベルに配置すると、このアスペクトはメソッド呼び出しと一致します。

@Around("@annotation(someAnnotation)") 
public Object invokeService(ProceedingJoinPoint pjp, SomeAnnotation someAnnotation) 
throws Throwable { 
    // need to have access to someAnnotation's parameters. 
    someAnnotation.value(); 

}

私は春とプロキシベースの側面を使用しています。

答えて

3

次の作業をする必要があり

@Pointcut("@target(someAnnotation)") 
public void targetsSomeAnnotation(@SuppressWarnings("unused") SomeAnnotation someAnnotation) {/**/} 

@Around("targetsSomeAnnotation(someAnnotation) && execution(* *(..))") 
public Object aroundSomeAnnotationMethods(ProceedingJoinPoint joinPoint, SomeAnnotation someAnnotation) throws Throwable { 
    ... your implementation.. 
} 
1

@targetを使用して、反射を使ってタイプレベルの注釈を読み込みます。

@Around("@target(com.example.SomeAnnotation)") 
public Object invokeService(ProceedingJoinPoint pjp) throws Throwable { 
関連する問題