Java Spring 中的 ProxyFactoryBean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3489428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ProxyFactoryBean in Spring
提问by javaguy
Can someone explain ProxyFactoryBeanin simple terms?
有人可以用简单的术语解释ProxyFactoryBean吗?
I see this being quoted lot of places.
我看到很多地方都引用了这一点。
采纳答案by skaffman
ProxyFactoryBean
is used to apply interceptor logic to an existing target bean, so that when methods on that bean are invoked, the interceptors are executed before-and-after that method call. This is an example of Aspect Oriented Programming (AOP).
ProxyFactoryBean
用于将拦截器逻辑应用于现有目标 bean,以便在调用该 bean 上的方法时,拦截器在该方法调用之前和之后执行。这是一个面向方面编程(AOP)的例子。
This is best explained using a simple example. A classic use-case for AOP is to apply caching to the result of a method call. This could be wired up using ProxyFactoryBean
as follows:
最好用一个简单的例子来解释这一点。AOP 的一个经典用例是对方法调用的结果应用缓存。这可以使用ProxyFactoryBean
如下连接:
<bean id="targetService" class="com.x.MyClass"/>
<bean id="cachingInterceptor" class="com.x.MyCachingInterceptor"/>
<bean id="cachedService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="targetService"/>
<property name="interfaces">
<list>
<value>com.x.MyService</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>cachingInterceptor</value>
</list>
</property>
</bean>
We have a bean targetService
of type com.x.MyClass
, which implements the interface com.x.MyService
. We also have a interceptor bean called cachingInterceptor
, which implements the interface org.aopalliance.intercept.MethodInterceptor
.
我们有一个targetService
类型为 的 bean com.x.MyClass
,它实现了接口com.x.MyService
。我们还有一个名为 的拦截器 bean cachingInterceptor
,它实现了接口org.aopalliance.intercept.MethodInterceptor
。
This config will generate a new bean, called cachedService
, which implements the MyService
interface. Any calls to the methods on that object will first be passed through the cachingInterceptor
object's invoke()
method, which in this case would look for the results of previous method calls in its internal cache. It would either return the cached result, or allow the method call to proceed to the appropropriate method on targetService
.
此配置将生成一个名为 的新 bean cachedService
,它实现了该MyService
接口。对该对象上方法的任何调用将首先通过cachingInterceptor
对象的invoke()
方法传递,在这种情况下,它将在其内部缓存中查找先前方法调用的结果。它要么返回缓存的结果,要么允许方法调用继续到 上的适当方法targetService
。
targetService
itself knows nothing of this, it's completely unaware of all this AOP stuff going on.
targetService
它自己对此一无所知,它完全不知道发生的所有这些 AOP 事情。
ProxyFactoryBean
is heavily used internally within Spring to generate proxies for a variety of reasons (e.g. remoting stubs, transaction management), but it's perfectly suitable for use in application logic also.
ProxyFactoryBean
由于各种原因(例如远程存根、事务管理),在 Spring 内部大量使用它来生成代理,但它也非常适合在应用程序逻辑中使用。
回答by mdma
The ProxyFactoryBean applies aspects to an existing bean. You start out with your existing bean (the target bean), which spring "wraps" to add the aspects you provide. The returned bean has the same interface as your original bean, but with the additional aspects weaved around the target bean's methods.
ProxyFactoryBean 将方面应用于现有 bean。您从现有的 bean(目标 bean)开始,它会“包装”以添加您提供的方面。返回的 bean 与原始 bean 具有相同的接口,但围绕目标 bean 的方法编织了其他方面。