强制 Spring Boot 2 使用 JDK 代理失败
Forcing Spring Boot 2 to use JDK Proxy failed
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";
}
result: class
weatherReport.entity.Hello$$EnhancerBySpringCGLIB$$b853a6c3
application.properties
spring.aop.auto=true
spring.aop.proxy-target-class=false
好吧,我错过了一些关于 JDK Proxy 的重要理论,目标 class 应该实现接口然后我们可以使用 JDK Proxy。
在我的代码中,weatherservice 实现了一个接口,当我将 spring.aop.proxy-target-class 设置为 false 时,Spring Boot 2 使用 JDK Proxy:
class com.sun.proxy.$Proxy62
但是当我将 spring.aop.proxy-target-class 设置为 true 时,Spring Boot2 使用默认的 cglib 代理:
class weatherReport.service.impl.WeatherQueryServiceImpl$$EnhancerBySpringCGLIB$$40d58c6
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";
}
result: class weatherReport.entity.Hello$$EnhancerBySpringCGLIB$$b853a6c3
application.properties
spring.aop.auto=true
spring.aop.proxy-target-class=false
好吧,我错过了一些关于 JDK Proxy 的重要理论,目标 class 应该实现接口然后我们可以使用 JDK Proxy。 在我的代码中,weatherservice 实现了一个接口,当我将 spring.aop.proxy-target-class 设置为 false 时,Spring Boot 2 使用 JDK Proxy:
class com.sun.proxy.$Proxy62
但是当我将 spring.aop.proxy-target-class 设置为 true 时,Spring Boot2 使用默认的 cglib 代理: class weatherReport.service.impl.WeatherQueryServiceImpl$$EnhancerBySpringCGLIB$$40d58c6