spring.aop.proxy-target-class=false
文件中的application.properties
无法帮助我强制Spring Boot2使用JDK代理。
方面
private Logger logger = LoggerFactory.getLogger(this.getClass());
private final String POINT_CUT = "execution(* weatherReport.entity.*.*(..)))";
@Pointcut(POINT_CUT)
private void pointcut() {}
@Before(value="pointcut()")
public void before(JoinPoint pjp) {
logger.info(" Check for user access ");
logger.info(" Allowed execution for {}", pjp);
}
目标组件:
@Component
public class Hello {
public String name = "default";
public String helloStr = "Guys";
public void saySomething() {
System.out.println(this.name+":"+this.helloStr);
}
}
控制器:
@Autowired
private WeatherQueryService weatherservice;
@Autowired
private Hello hello;
@RequestMapping(value="/hello")
public String sayHello() {
System.out.println(weatherservice);
System.out.println(hello.getClass());
hello.saySomething();
System.out.println(hello.getClass());
System.out.println(weatherservice.getClass());
return "hello world";
}
结果:class weatherReport.entity.Hello $$ EnhancerBySpringCGLIB $$ b853a6c3
application.properties
spring.aop.auto=true
spring.aop.proxy-target-class=false
好吧,我错过了一些关于JDK代理的重要理论,目标类应该实现接口然后我们可以使用JDK代理。在我的代码中,weatherservice实现了一个接口,当我将spring.aop.proxy-target-class设置为false时,Spring Boot 2使用JDK代理:
class com.sun.proxy。$ Proxy62
但是当我将spring.aop.proxy-target-class设置为true时,Spring Boot2使用默认的cglib代理:class weatherReport.service.impl.WeatherQueryServiceImpl $$ EnhancerBySpringCGLIB $$ 40d58c6