码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Proxying Mechanisms_Spring代理机制

时间:2014-11-05 13:14:40      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:style   io   color   ar   os   for   sp   strong   on   

Spring Proxying Mechanisms_Spring代理机制

来自:/current/spring-framework-reference4.1.1

Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. (JDK dynamic proxies are preferred whenever you have a choice).


If the target object to be proxied implements at least one interface then a JDK dynamic proxy will be used. All of the interfaces implemented by the target type will be proxied. If the target object does not implement any interfaces then a CGLIB proxy will be created.


If you want to force the use of CGLIB proxying (for example, to proxy every method defined for the target object, not just those implemented by its interfaces) you can do so. However, there are some issues to consider:

  1. final methods cannot be advised, as they cannot be overridden.

  2. As of Spring 3.2, it is no longer necessary to add CGLIB to your project classpath, as CGLIB classes are repackaged under org.springframework and included directly in the spring-core JAR. This means that CGLIB-based proxy support just works in the same way that JDK dynamic proxies always have.

  3. The constructor of your proxied object(被代理的对象) will be called twice. This is a natural consequence(结果) of the CGLIB proxy model whereby(通过) a subclass(子类) is generated for each proxied object. For each proxied instance, two objects are created: the actual proxied object and an instance of the subclass that implements the advice. This behavior is not exhibited(陈列展览) when using JDK proxies. Usually, calling the constructor of the proxied type twice, is not an issue, as there are usually only assignments taking place and no real logic is implemented in the constructor.

To force the use of CGLIB proxies set the value of the proxy-target-class attribute of the <aop:config> element to true:

<aop:config proxy-target-class="true">
    <!-- other beans defined here... -->
</aop:config>

To force CGLIB proxying when using the @AspectJ autoproxy support, set the ‘proxy-target-class‘ attribute of the <aop:aspectj-autoproxy> element to true:

<aop:aspectj-autoproxy proxy-target-class="true"/>

====END====


Spring Proxying Mechanisms_Spring代理机制

标签:style   io   color   ar   os   for   sp   strong   on   

原文地址:http://my.oschina.net/xinxingegeya/blog/340943

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!