I have created a Spring-Boot application
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Slf4j
public class AOPTestApplication {
public static void main(String[] args) {
SpringApplication.run(AOPTestApplication.class, args);
}
}
I have defined an annotation @Parameter to be placed before an argument
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Parameter {
}
In order to handle such an annotation with Spring AOP, I have created an aspect as a component
@Component
@Aspect
@Slf4j
public class ParameterAspect {
@Around("execution(* *(.., @Parameter (java.lang.String), ..))")
public Object executeMethodWithParameterArg(ProceedingJoinPoint point) throws Throwable {
log.warn("Method " + point.toShortString() + " has been intercepted");
return point.proceed();
}
}
Now, I have defined two use cases :
- I call an endpoint which calls a service (where @Parameter is defined)
- I call an endpoint which calls a local methods (where @Parameter is defined)
Now the RestController
@RestController
@RequestMapping("/parameter/test")
@Slf4j
public class ParameterTestRestController {
@Autowired
private ParameterService parameterService;
@RequestMapping(value = "/1", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public void test_1() {
String arg1 = "ARG1";
String arg2 = "ARG2";
log.warn("(BEFORE CALL) ARG1 = " + arg1 + ", ARG2 = " + arg2);
parameterService.test(arg1, arg2);
}
@RequestMapping(value = "/2", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public void test_2() {
String arg1 = "ARG1";
String arg2 = "ARG2";
log.warn("(BEFORE CALL) ARG1 = " + arg1 + ", ARG2 = " + arg2);
test_2(arg1, arg2);
}
public void test_2(String arg1, @Parameter String arg2) {
log.warn("(INSIDE METHOD) ARG1 = " + arg1 + ", ARG2 = " + arg2);
}
}
And the service
@Service
@Slf4j
public class ParameterService {
public void test(@Parameter String arg1, String arg2) {
log.warn("(INSIDE METHOD) ARG1 = " + arg1 + ", ARG2 = " + arg2);
}
}
The method test_1() is well handled by the aspect. But the method test_2() is not.
Why ?