ProxyFactoryBean in Spring
Asked Answered
A

2

41

Can someone explain ProxyFactoryBean in simple terms?

I see this being quoted lot of places.

Auntie answered 15/8, 2010 at 21:52 Comment(1)
Have you seen "Gluing it All Together" in javalobby.org/java/forums/t44746.html?Crystallite
S
56

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.

Smash answered 16/8, 2010 at 7:58 Comment(1)
Just one thing to add here, is that if we have several interceptorNames they are applied in such an order, that the first one in the list is the first to intercept the invocation and so on.Revolutionist
A
7

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.

Amaya answered 15/8, 2010 at 23:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.