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).
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:
com.x.MyService
cachingInterceptor
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.