Gets the target object of the JDK dynamic Agent/cglib proxy object agent.

Source: Internet
Author: User

Problem Description::

I now have a tricky problem, to save the object through the spring managed service class, which is obtained through reflection, and it is experimentally discovered that the class can only reflect the method that Sservice implements the interface, and the methods of the extends class do not appear. Debug found that this Servie instance was replaced by spring with the Jdkdynmicproxy class, rather than the original object, it is only the service inherited interface method, and there is no extends super class method, how to invoke the native object method !!!!!

Call the GetClass (). GetName () method with the managed Spring service class, and find out that the output is $proxy43 such things!!


import Java.lang.reflect.Field; import Org.springframework.aop.framework.AdvisedSupport; import org.springframework.aop.framework.AopProxy; import org.springframework.aop.support.AopUtils; /*** Description: Can obtain the target object of the JDK dynamic Agent/cglib proxy object agent. * All rights Reserved.* @version 1.0-6-28 a.m. 9:04:32 by zhangbo01@zuche. com creation */public class aoptargetutils {      /** * Get target object* @param proxy Object* @return * @throws Exception     */  Public static object Gettarget (object proxy) throws Exception {           if(! Aoputils. Isaopproxy (proxy)) {   return proxy; //Not a proxy object         }             if(aoputils. Isjdkdynamicproxy(proxy)) { return getjdkdynamicproxytargetobject(proxy); } Else { //cglib return getcglibproxytargetobject(proxy);         }                                    }       private static object Getcglibproxytargetobject (object proxy) throws Exception {Field h = Proxy.getclass (). Getdeclaredfield ("Cglib$callback_0"); h.setaccessible (true); Object dynamicadvisedinterceptor = h.get (proxy);           Field advised = Dynamicadvisedinterceptor.getclass (). Getdeclaredfield ("advised"); advised.setaccessible (true);           Object target = ((Advisedsupport) Advised.get (Dynamicadvisedinterceptor)). Gettargetsource (). Gettarget ();            return target;     }       private static object Getjdkdynamicproxytargetobject (object proxy) throws Exception { Field h = Proxy.getclass (). Getsuperclass (). Getdeclaredfield ("H"); h.setaccessible (true); aopproxy aopproxy = (aopproxy) h.get (proxy);           Field advised = Aopproxy.getclass (). Getdeclaredfield ("advised"); advised.setaccessible (true);           Object target = ((Advisedsupport) Advised.get (Aopproxy)). Gettargetsource (). Gettarget ();            return target;     }        }

From for notes (Wiz)

Gets the target object of the JDK dynamic Agent/cglib proxy object agent.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.