Targeting aspects based annotation on a class with Spring and AspectJ
annotations, aop, aspectj, java, spring
Solution
The following should work
@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..
}
Problem
How to make an aspect that targets all public methods that belong to a class that is marked with specific annotation? In following method1() and method2() should be processed by the aspect and method3() should not be processed by the aspect. ``` @SomeAnnotation(SomeParam.class) public class FooServiceImpl extends FooService { public void method1() { ... } public void method2() { ... } } public class BarServiceImpl extends BarService { public void method3() { ... } } ``` If I put annotations on method level, this aspect will work and match the method calls. ``` @Around("@annotation(someAnnotation)") public Object invokeService(ProceedingJoinPoint pjp, SomeAnnotation someAnnotation) throws Throwable { // need to have access to someAnnotation's parameters. someAnnotation.value(); ``` } I am using Spring and proxy based aspects.