I've been browsing the web and stackoverflow for hours now and I can't seem to find a solution to my problem. My guess is that it's something tiny but hopefully obvious to those more proficient with AOP than I am.
We're using Spring Boot 1.2.6.RELEASE for our application and I want to include a custom annotation called @Prohibited in order to be able to execute an @Around advice according to the user's permission settings and the annotation's values.
So far, I only have a blank annotation and I want my advice to be executed around any method annotated with @Prohibited.
The annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface Prohibited {
}
The aspect:
@Aspect
@Component
public class PermissionAspect {
@Pointcut("execution(@com.whatever.commons.logic.permission.Prohibited * *(..))")
public void prohibited() {}
@Around("prohibited()")
public Object permit(final ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("########### - " + joinPoint.getSignature().getName());
joinPoint.proceed();
return null;
}
}
The annotated method:
@Component
public class AnyService {
@Prohibited
public List<Object> anyMethod() {
//anything - assume a breakpoint here
}
}
In my pom.xml I have included the spring-boot-starter-aop dependency:
<!-- spring boot with aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
And finally in the application.properties file I have added the following line in order to enable Spring to create proxies around classes, too, and not just interfaces:
spring.aop.proxy-target-class=true
Now I set a breakpoint to the first line of the annotated method and would expect to see at least the print out from my advice in the console, but it never happens.
Instead the application suspends at the breakpoint without any lines printed. Interestingly enough, I can see in the call stack that there is indeed a proxy created for the class and I can see things such as:
CglibAopProxy$CglibMethodInvocation.invokeJoinpoint() line: 717
CglibAopProxy$CglibMethodInvocation(ReflectiveMethodInvocation).proceed() line: 157
If I comment out the @Prohibited annotation, no proxy is generated at all, so it appears that while Spring does create a CGLIB proxy around my designated class (apparently due to the @Pointcut defined in my aspect), but it does not execute my @Around advice, yet instead just proceeds to the joinpoint.
Does anyone have an idea what I am missing here? Should I work with compile-time weaving (I'd like to avoid that, if possible)?
* UPDATE *
What I forgot to mention was how the method is actually called. From another service (aka a controller in this case):
@RestController
public class AnyController {
@Autowired
AnyService anyService;
@RequestMapping("/this/that")
public ResponseEntity<Object> something() {
this.anyService.anyMethod();
}
}
The problem was that I annotated the wrong method (see answer below).