I want to do AOP on below @RequestMapping method call, notice the hello() method is NOT public.
@RestController
class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String hello() {
return "Hello";
}
}
Here is the main class, I have added @Aspect, @EnableAspectJAutoProxy annotation.
@Aspect
@EnableAspectJAutoProxy(proxyTargetClass = true)
@SpringBootApplication
public class FooServiceApplication {
public static void main(String[] args) {
SpringApplication.run(FooServiceApplication.class, args);
}
@Around("@annotation(requestMapping)")
public Object around(ProceedingJoinPoint pjp, RequestMapping requestMapping) throws Throwable {
return pjp.proceed();
}
}
in pom.xml I just add below dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
The result is that if hello() method is public, the AOP will just works fine, but if the same as the example above without public declaration, the AOP will not work at all. But isn't EnableAspectJAutoProxy will use CGLIB and can intercept protected / private method calls?