首页 > 其他分享 >如何获取spring aop代理类的被代理类或者说是获取目标类

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

时间:2023-02-21 17:33:55浏览次数:35  
标签:spring 代理 获取 singletonTarget retryThread ConsulRetryRegistry

@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


标签:spring,代理,获取,singletonTarget,retryThread,ConsulRetryRegistry
From: https://www.cnblogs.com/yangxiaohui227/p/17141784.html

相关文章