PDA

View Full Version : ProxyFactoryBean proxying singleton


bst@jcs.be
Nov 9th, 2004, 04:37 AM
I have a ProxyFactoryBean proxing a bean which is created using a MethodInvokingFactoryBean.

MethodInvokingFactoryBean works fine. However, when I wrap it in a ProxyFactoryBean the latter creates a NEW instance (I see the no-arg constructor runs) of the bean originally returned by the MethodInvokingFactoryBean, it does not wrap at all this original. Moreover, ProxyFactoryBean decides to go to the constructor directly, bypassing our factory.

Why isn't ProxyFactoryBean proxying the original bean returned by MethodInvokingFactoryBean?!

Bart

<bean id="simpleApplicationServiceDelegateFactory" class="com.vangenechten.common.j2ee.SimpleApplicationServ iceDelegateFactoryFactoryBean">
</bean>

<bean id="mdasd-naked" class="org.springframework.beans.factory.config.MethodInv okingFactoryBean" singleton="true" >
<property name="targetObject">
<ref local="simpleApplicationServiceDelegateFactory"/>
</property>
<property name="targetMethod">
<value>getApplicationServiceDelegate</value>
</property>
<property name="arguments">
<list>
<value>com.vangenechten.masterdata.client.masterdata.Mast erDataApplicationServiceDelegate</value>
</list>
</property>
</bean>

<bean id="mdasd" class="org.springframework.aop.framework.ProxyFactoryBean" singleton="true" >
<property name="target">
<ref local="mdasd-naked"/>
</property>
<property name="interceptorNames">
<list>
<value>commitInterceptor</value>
<value>commitThrowsAdvice</value>
</list>
</property>
</bean>

Juergen Hoeller
Nov 20th, 2004, 02:03 PM
Hi Bart,

The problem is that you're not specifying a proxy interface for your ProxyFactoryBean, so you force it to create a CGLIB proxy (assuming that your target object as created by MethodInvokingFactoryBean does not implement any interfaces).

A CGLIB proxy is technically an instance of a subclass of your target class, so what you will see is in fact another call to the constructor of your target class. That's the way CGLIB works; this can indeed incur unintuitive effects, unfortunately.

I recommend to let your target object implement a business interface and specify a corresponding "proxyInterfaces" value on your ProxyFactoryBean definition, to enforce a JDK proxy rather than a CGLIB proxy.

Juergen

bst@jcs.be
Nov 22nd, 2004, 10:21 AM
proxyInterfaces solved the problem. Thanks!