简体   繁体   中英

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.

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

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. 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:

class 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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