Can someone explain ProxyFactoryBean in simple terms?
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. This is an example of Aspect Oriented Programming (AOP).
In common with most FactoryBean implementations provided with Spring, the ProxyFactoryBean class is itself a JavaBean. Its properties are used to: Specify the target you want to proxy. Specify whether to use CGLIB (see below and also the section entitled Section 8.5. 3, “JDK- and CGLIB-based proxies”).
1. What is Spring AOP? Spring AOP enables Aspect-Oriented Programming in spring applications. In AOP, aspects enable the modularization of concerns such as transaction management, logging or security that cut across multiple types and objects (often termed crosscutting concerns).
CGLIB is a powerful, high performance code generation library. It is complementary to the JDK dynamic proxy in that it provides proxying classes that do not implement interfaces. Under the covers, it uses ASM bytecode manipulation framework.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With