Casting a Spring's Proxy object to the actual runtime class

前端 未结 3 1595
夕颜
夕颜 2020-12-09 10:52

I\'m using Spring, at one point I would like to cast the object to its actual runtime implementation.

Example:

Class MyClass extends NotMyClass {
            


        
3条回答
  •  失恋的感觉
    2020-12-09 11:57

    For me version from EDIT 2 didn't worked. Below one worked:

    @SuppressWarnings({"unchecked"})
    protected  T getTargetObject(Object proxy) throws Exception {
        while( (AopUtils.isJdkDynamicProxy(proxy))) {
            return (T) getTargetObject(((Advised)proxy).getTargetSource().getTarget());
        }
        return (T) proxy; // expected to be cglib proxy then, which is simply a specialized class
    }
    

    Usage:

        UserServicesImpl serviceImpl = getTargetObject(serviceProxy);
        serviceImpl.setUserDao(userDAO);
    

提交回复
热议问题