在Spring AOP中使用代理 [英] Use of proxies in Spring AOP

查看:153
本文介绍了在Spring AOP中使用代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读一本关于在Spring AOP中启用 AspectJ 支持的书。

I am reading a book, which talks about enabling AspectJ support in Spring AOP.

以下是本书中的段落:


要在Spring IoC容器中启用AspectJ注释支持,您只需要定义一个空的
XML元素aop:aspectj bean配置文件中的-autoproxy。然后,Spring将自动
为您的AspectJ方面匹配的任何bean创建代理。

To enable AspectJ annotation support in the Spring IoC container, you only have to define an empty XML element aop:aspectj-autoproxy in your bean configuration file. Then, Spring will automatically create proxies for any of your beans that are matched by your AspectJ aspects.

对于接口不可用的情况或者不在应用程序的设计中使用,可以通过依赖CGLIB来
创建代理。要启用CGLIB,您需要在< aop:aspectj-autoproxy /> proxy-target-class = true c $ c>。

For cases in which interfaces are not available or not used in an application’s design, it’s possible to create proxies by relying on CGLIB. To enable CGLIB, you need to set the attribute proxy-target-class=true in <aop:aspectj-autoproxy />.



我无法获得第二段。 接口的含义不可用'。任何人都可以用一个例子说明这一点吗?


I am not able to get the second paragraph. What is meant by 'interfaces are not available'. Can anyone illustrate this with an example ?

推荐答案

Spring AOP使用JDK动态代理或CGLIB为目标对象创建代理。

Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxies for your target objects.

根据Spring文档,如果您的目标实现至少一个接口,将使用JDK动态代理。但是,如果您的目标对象没有实现任何接口,那么将创建一个CGLIB代理。

According to Spring documentation, in case your target implements at least one interface, a JDK dynamic proxy will be used. However if your target object does not implement any interfaces then a CGLIB proxy will be created.

这是强制创建CGLIB代理的方法(设置代理 - 目标 - class = true ):

This is how you can force creation of the CGLIB proxies (set proxy-target-class="true"):

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

使用AspectJ及其autopoxy支持时,您也可以强制使用CGLIB代理。这是使用< aop:aspectj-autoproxy> 的地方,此处proxy-target-class必须设置为 true

When using AspectJ and its autopoxy support you can also force CGLIB proxies. This is where the <aop:aspectj-autoproxy> is used and also here the "proxy-target-class" must be set to true:

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

请参阅使用Spring的面向对象编程文档了解更多详情。

Please refer to Proxying mechanisms section of Aspect Oriented Programming with Spring documentation for more details.

这篇关于在Spring AOP中使用代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆