About Spring Dynamic agents

Source: Internet
Author: User

By default, Spring generates agents according to the following rules:

When the class has an implementation interface, Spring generates the Jdkdynamicaopproxy proxy

When the class does not implement an interface, Spring generates the Cglibaopproxy proxy

If you want to force spring to generate Cglibaopproxy proxies, you can <aop:config proxy-target-class= "true" > </aop:config>

When you need to use the Cglib Proxy and @aspectj automatic proxy support, set <aop:aspectj-autoproxy> the properties as follows proxy-target-class : <aop:aspectj-autoproxy proxy-target-class= "true" />

---------------------------------------------------------------------when implementing an agent through Jdkdynamicaopproxy, How to get to the target object-------------------------------------------------------------

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;
}

--------------------------------------------------------------------when implementing an agent through Cglibaopproxy, How to get the target object---------------------------------------------------------------------------

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;
}

About Spring Dynamic agents

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.