I have following aspect class:
@Component
@Aspect
public class LoggedRequestAspect {
private static final Logger LOG = LoggerFactory.getLogger(LoggedRequestAspect.class);
@Before("com.application.aspect.pointcut.LoggedRequestPointCut.LogRequest()")
public void logRequest(){
System.out.println("Method Executed!");
LOG.debug("Method Executed!");
}
}
And for Annotation class:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LoggedRequest {
}
And finally pointcut:
public class LoggedRequestPointCut {
@Pointcut("execution(@com.application.aspect.annotation.LoggedRequest * *(..))")
public void LogRequest(){}
}
Now for sure, I have annotated the required api method in my controller:
...
@LoggedRequest
@RequestMapping(value = "/login", method = { RequestMethod.POST })
public @ResponseBody Map<String, Object> login(/* Param List*/){
...
}
As suggested by many other answers on SO. I have added following to my spring configuration:
<context:annotation-config/>
...
<context:component-scan base-package="com.application.core"/>
...
<aop:aspectj-autoproxy />
Now All this is not working. I mean on my API call of /login the required advice is not executing.
<aop:aspectj-autoproxy />? Because if that tis in your root application context (i.e.ContextLoaderListener) it is going to do exactly nothing for your controllers. Next to that you would have to enableproxy-target-class="true"on the<aop:aspectj-autoproxy />as well to make it work for controllers. - M. Deinum@Pointcut(execution(@com.application.aspect.annotation.LoggedRequest * *(..))to@Pointcut(@annotation(com.application.aspect.annotation.LoggedRequest))and then try to invoke your annotated methods. - mlewandowskispring-aopandaspectjweaverin your classpath? - MichaĆ KosmulskiDispatcherServlet. AOP only works on beans in the same application context not on beans in parent or child contexts. - M. Deinum