简体   繁体   English

强制Spring Boot 2使用JDK代理失败

[英]Forcing Spring Boot 2 to use JDK Proxy failed

spring.aop.proxy-target-class=false in application.properties file doesn't help me to force Spring Boot2 to use JDK Proxy. application.properties文件中的spring.aop.proxy-target-class=false不能帮助我强制Spring Boot2使用JDK代理。

Aspect 方面

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);
}

Target Component: 目标成分:

@Component
public class Hello {
    public String name = "default";
    public  String helloStr = "Guys";
    public void saySomething() {
        System.out.println(this.name+":"+this.helloStr);
    }
}

Controller: 控制器:

@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 结果:类weatherReport.entity.Hello $$ EnhancerBySpringCGLIB $$ b853a6c3

application.properties application.properties

spring.aop.auto=true
spring.aop.proxy-target-class=false

Okay I missed some important theory about JDK Proxy, the target class should implement interface then we can use JDK Proxy. 好的,我错过了一些有关JDK Proxy的重要理论,目标类应该实现接口,然后我们才能使用JDK Proxy。 In my code, weatherservice implements an interface, and when I set spring.aop.proxy-target-class to be false, Spring Boot 2 uses JDK Proxy: 在我的代码中,weatherservice实现了一个接口,当我将spring.aop.proxy-target-class设置为false时,Spring Boot 2使用了JDK Proxy:

class com.sun.proxy.$Proxy62 com.sun.proxy。$ Proxy62类

but when I set spring.aop.proxy-target-class to be true, Spring Boot2 uses default cglib Proxy: class weatherReport.service.impl.WeatherQueryServiceImpl$$EnhancerBySpringCGLIB$$40d58c6 但是当我将spring.aop.proxy-target-class设置为true时,Spring Boot2使用默认的cglib代理:class weatherReport.service.impl.WeatherQueryServiceImpl $$ EnhancerBySpringCGLIB $$ 40d58c6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM