我已经创建了一个Spring-Boot应用程序
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Slf4j
public class AOPTestApplication {
public static void main(String[] args) {
SpringApplication.run(AOPTestApplication.class, args);
}
}
我已经定义了一个注释@Parameter放在一个参数
之前@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Parameter {
}
为了使用Spring AOP处理这样的注释,我创建了一个方面作为组件
@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();
}
}
现在,我已经定义了两个用例:
现在是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);
}
}
服务
@Service
@Slf4j
public class ParameterService {
public void test(@Parameter String arg1, String arg2) {
log.warn("(INSIDE METHOD) ARG1 = " + arg1 + ", ARG2 = " + arg2);
}
}
方法test_1()由方面处理得很好。但方法test_2()不是。
为什么?