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

时间:2019-03-12 09:08:41

标签: java spring-boot spring-aop

spring.aop.proxy-target-class=falseapplication.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";
}
  

结果:类   weatherReport.entity.Hello $$ EnhancerBySpringCGLIB $$ b853a6c3

application.properties

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

1 个答案:

答案 0 :(得分:0)

好吧,我错过了一些有关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