-1

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.

Muhammad Waqas
  • 367
  • 3
  • 13

1 Answers1

0

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 {


    }
}
Amit Naik
  • 983
  • 1
  • 5
  • 16
  • 2
    I have no idea why the OP accepted this answer because IMO it does not solve the problem. The additional `execution(public * *(..)) and ` does not change the aspect's behaviour in any way. So whatever solved the OP's problem, it must have been something else. I am just commenting on this so that nobody believes it is a real solution. For example, it is much more likely that something like a fully-qualified class name such as `@annotation(my.package.MyCustomAnnotation)` actually solved it. – kriegaex Apr 24 '19 at 08:18