Can someone explain ProxyFactoryBean in simple terms?
I see this being quoted lot of places.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
ProxyFactoryBeanis 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
ProxyFactoryBeanas follows:We have a bean
targetServiceof typecom.x.MyClass, which implements the interfacecom.x.MyService. We also have a interceptor bean calledcachingInterceptor, which implements the interfaceorg.aopalliance.intercept.MethodInterceptor.This config will generate a new bean, called
cachedService, which implements theMyServiceinterface. Any calls to the methods on that object will first be passed through thecachingInterceptorobject’sinvoke()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 ontargetService.targetServiceitself knows nothing of this, it’s completely unaware of all this AOP stuff going on.ProxyFactoryBeanis 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.