如何获取spring aop代理类的被代理类或者说是获取目标类

@RestController
public class TestController {
    @Autowired
    private ConsulRetryRegistry consulRetryRegistry;


    @RequestMapping("server/offline")
    public String doDeRegister(){

        ConsulRetryRegistry singletonTarget =(ConsulRetryRegistry) AopProxyUtils.getSingletonTarget(consulRetryRegistry);
        singletonTarget.retryThread.doRegister();
        return "ok";

    }
}

注意:当直接注入ConsulRetryRegistry时,注入的是代理类,而代理类中的 retryThread属性字段是空的,所以需要获取被代理类,通过spring自带的util:  AopProxyUtils


posted @ 2023-02-21 17:23  yangxiaohui227  阅读(468)  评论(0编辑  收藏  举报