简体   繁体   English

Spring 中的 ProxyFactoryBean

[英]ProxyFactoryBean in Spring

Can someone explain ProxyFactoryBean in simple terms?有人可以用简单的术语解释ProxyFactoryBean吗?

I see this being quoted lot of places.我看到很多地方都引用了这句话。

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. ProxyFactoryBean用于将拦截器逻辑应用于现有目标 bean,以便在调用该 bean 上的方法时,拦截器在该方法调用之前和之后执行。 This is an example of Aspect Oriented Programming (AOP).这是面向方面编程 (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. AOP 的一个经典用例是将缓存应用于方法调用的结果。 This could be wired up using ProxyFactoryBean as follows:这可以使用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 .我们有一个类型为com.x.MyClass的 bean targetService ,它实现了接口com.x.MyService We also have a interceptor bean called cachingInterceptor , which implements the interface org.aopalliance.intercept.MethodInterceptor .我们还有一个名为cachingInterceptor的拦截器 bean,它实现了接口org.aopalliance.intercept.MethodInterceptor

This config will generate a new bean, called cachedService , which implements the MyService interface.此配置将生成一个名为cachedService的新 bean,它实现了MyService接口。 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.对该对象的任何方法调用都将首先通过cachingInterceptor对象的invoke()方法传递,在这种情况下,该方法会在其内部缓存中查找先前方法调用的结果。 It would either return the cached result, or allow the method call to proceed to the appropropriate method on targetService .它要么返回缓存的结果,要么允许方法调用继续进行到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 (eg remoting stubs, transaction management), but it's perfectly suitable for use in application logic also. ProxyFactoryBean在 Spring 内部大量使用,出于各种原因(例如,远程处理存根、事务管理)生成代理,但它也非常适合在应用程序逻辑中使用。

The ProxyFactoryBean applies aspects to an existing bean. ProxyFactoryBean 将方面应用于现有 bean。 You start out with your existing bean (the target bean), which spring "wraps" to add the aspects you provide.您从现有的 bean(目标 bean)开始,它会“包装”以添加您提供的方面。 The returned bean has the same interface as your original bean, but with the additional aspects weaved around the target bean's methods.返回的 bean 具有与原始 bean 相同的接口,但具有围绕目标 bean 的方法编织的其他方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM