ProxyFactoryBean in Spring

前端 未结 2 640
轻奢々
轻奢々 2021-01-30 11:06

Can someone explain ProxyFactoryBean in simple terms?

I see this being quoted lot of places.

相关标签:
2条回答
  • 2021-01-30 11:21

    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).

    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:

    <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.

    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.

    targetService itself knows nothing of this, it's completely unaware of all this AOP stuff going on.

    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.

    0 讨论(0)
  • 2021-01-30 11:30

    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.

    0 讨论(0)
提交回复
热议问题