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

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

问题描述

我正在读一本书,其中谈到在 Spring AOP 中启用 AspectJ 支持.

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

以下是书中摘录的一段:

Given below is a paragraph taken from the book:

要在 Spring IoC 容器中启用 AspectJ 注解支持,您只需定义一个空的bean 配置文件中的 XML 元素 aop:aspectj-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.

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 代理的方法(设置 proxy-target-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 及其自动代理支持时,您还可以强制使用 CGLIB 代理.这是使用 的地方,而且这里的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的面向切面编程<的代理机制部分/a> 文档了解更多详情.

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

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

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