I have an annotation.
@Target(value = {ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyCustomAnnotation{
}
My Aspect class is like that
@Component
@Aspect
public class MyCustomAsspect{
@AfterReturning(
pointcut="@annotation(MyCustomAnnotation)",
returning="retVal")
public void publishMessage(JoinPoint jp, Object retVal) throws Throwable {
}
}
My Service class is
@Service
public class ServiceClass{
@MyCustomAnnotation
public Object someMethod(){
return new Object();
}
}
Above are mentioned classes i am not sure why my aspect not working. I am new to Spring AOP . Please help me it shall be very thankful.
Issue is due to pointcut declaration. As spring documentation says
@annotation - limits matching to join points where the subject of the join point (method being executed in Spring AOP) has the given annotation
So I order to make this work
@Aspect
public class MyCustomAsspect{
@AfterReturning(
pointcut="execution(public * *(..)) and @annotation(MyCustomAnnotation)",
returning="retVal")
public void publishMessage(JoinPoint jp, Object retVal) throws Throwable {
}
}