![](/img/trans.png)
[英]Forcing Spring Boot to NOT use EmbeddedWebApplicationContext?
[英]Forcing Spring Boot 2 to use JDK Proxy failed
application.properties
文件中的spring.aop.proxy-target-class=false
不能幫助我強制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";
}
結果:類weatherReport.entity.Hello $$ EnhancerBySpringCGLIB $$ b853a6c3
application.properties
spring.aop.auto=true
spring.aop.proxy-target-class=false
好的,我錯過了一些有關JDK Proxy的重要理論,目標類應該實現接口,然后我們才能使用JDK Proxy。 在我的代碼中,weatherservice實現了一個接口,當我將spring.aop.proxy-target-class設置為false時,Spring Boot 2使用了JDK Proxy:
com.sun.proxy。$ Proxy62類
但是當我將spring.aop.proxy-target-class設置為true時,Spring Boot2使用默認的cglib代理:class weatherReport.service.impl.WeatherQueryServiceImpl $$ EnhancerBySpringCGLIB $$ 40d58c6
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.