Gets the target object tool class for the proxy object proxy in spring

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!!

Getting the target object in this way is unreliable, or any way to get the target object is unreliable, because the target object is stored in the Targetsource,targetsource, but Targetsource has many implementations, By default we use Singletontargetsource, but there are other things like Threadlocaltargetsource, Commonspooltargetsource and so on.

This is why spring does not provide the API to get the target object.

Java code
  1. Import Java.lang.reflect.Field;
  2. Import Org.springframework.aop.framework.AdvisedSupport;
  3. Import Org.springframework.aop.framework.AopProxy;
  4. Import Org.springframework.aop.support.AopUtils;
  5. Public class Aoptargetutils {
  6. /** 
  7. * Get target object
  8. * @param proxy Object
  9. * @return
  10. * @throws Exception
  11. */
  12. public Static Object Gettarget (object proxy) throws Exception {
  13. if (! Aoputils.isaopproxy (proxy)) {
  14. return proxy; //Not a proxy object
  15. }
  16. if (aoputils.isjdkdynamicproxy (proxy)) {
  17. return Getjdkdynamicproxytargetobject (proxy);
  18. } else { //cglib
  19. return Getcglibproxytargetobject (proxy);
  20. }
  21. }
  22. private Static object Getcglibproxytargetobject (object proxy) throws Exception {
  23. Field h = Proxy.getclass (). Getdeclaredfield ("cglib$callback_0");
  24. H.setaccessible (true);
  25. Object Dynamicadvisedinterceptor = h.get (proxy);
  26. Field advised = Dynamicadvisedinterceptor.getclass (). Getdeclaredfield ("advised");
  27. Advised.setaccessible (true);
  28. Object target = ((Advisedsupport) Advised.get (Dynamicadvisedinterceptor)). Gettargetsource (). Gettarget ();
  29. return target;
  30. }
  31. private Static object Getjdkdynamicproxytargetobject (object proxy) throws Exception {
  32. Field h = Proxy.getclass (). Getsuperclass (). Getdeclaredfield ("H");
  33. H.setaccessible (true);
  34. Aopproxy aopproxy = (aopproxy) h.get (proxy);
  35. Field advised = Aopproxy.getclass (). Getdeclaredfield ("advised");
  36. Advised.setaccessible (true);
  37. Object target = ((Advisedsupport) Advised.get (Aopproxy)). Gettargetsource (). Gettarget ();
  38. return target;
  39. }
  40. }

Gets the target object tool class for the proxy object proxy in spring

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.