2012-05-11 7 views
1

특정 주석으로 표시된 클래스에 속하는 모든 public 메소드를 대상으로하는 aspect를 만드는 방법은 무엇입니까? method1()방법 2()은 aspect로 처리해야하고 method3()은 aspect로 처리하면 안됩니다.Spring 및 AspectJ를 사용하여 클래스에서 주석 기반 aspect annotation을

@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 { 
관련 문제