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.