View Full Version : Proxying a BeanFactory
jdigger
Sep 15th, 2004, 09:33 AM
How can I proxy an object returned by a FactoryBean? Specificly, I want to put a DebugInterceptor around a RemoteStatelessSessionProxyFactoryBean. The problem is, it puts it around the FactoryBean, not what the FactoryBean returns (the proxy to the EJB)
Here's an example:
<bean id="serviceProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNa meAutoProxyCreator" lazy-init="false">
<property name="beanNames">
<value>userService,caseService,customerService,contactSer vice</value>
</property>
<property name="interceptorNames">
<list>
<value>debugInterceptor</value>
</list>
</property>
</bean>
<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugIntercept or" />
<bean id="userService" class="org.springframework.ejb.access.SimpleRemoteStatele ssSessionProxyFactoryBean" lazy-init="true" singleton="true">
<property name="businessInterface">
<value>mypackage.UserService</value>
</property>
<property name="jndiName">
<value>UserServiceBean-v1.6.0.0</value>
</property>
<property name="jndiTemplate">
<ref bean="jndiTemplate"/>
</property>
</bean>
What that produces is entries in my log of:
Debug interceptor: count=1 invocation=[Invocation: method=[public abstract java.lang.Object org.springframework.beans.factory.FactoryBean.getO bject() throws java.lang.Exception] args=null] target is of class [org.springframework.ejb.access.SimpleRemoteStatel essSessionProxyFactoryBean]]
Debug interceptor: next returned
... which isn't very helpful.
irbouho
Sep 15th, 2004, 09:28 PM
To proxy BeanFactory returned Object instead of BeanFactory itself you can apply your interceptors using ProxyFactoryBean:
<bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<bean class="org.springframework.ejb.access.SimpleRemoteStatele ssSessionProxyFactoryBean">
<property name="businessInterface">
<value>mypackage.UserService</value>
</property>
<property name="jndiName">
<value>UserServiceBean-v1.6.0.0</value>
</property>
<property name="jndiTemplate">
<ref bean="jndiTemplate"/>
</property>
</bean>
</property>
<property name="interceptorNames">
<list>
<value>debugInterceptor</value>
</list>
</property>
</bean>
jdigger
Sep 15th, 2004, 11:46 PM
Thanks for the suggested solution.
That works, but is there no way an autoproxy can be used? I've got a lot of services that I'd rather not have to manually apply the aspects to. :? Not being able to proxy FactoryBeans seems like a pretty significant limitation for the AOP framework considering how important FactoryBeans are to the Spring framework...
irbouho
Sep 16th, 2004, 12:19 AM
I do not know if this will make things easier for you. You can reduce the typing by using an abstract template and inheriting from it:
<!-- Requires Spring 1.1 -->
<bean id="interceptorStack" class="org.springframework.aop.framework.ProxyFactoryBean" abstract="true">
<property name="interceptorNames">
<list>
<value>debugInterceptor</value>
<value>interceptor2</value>
<value>interceptor3</value>
</list>
</property>
</bean>
<bean id="userService" parent="interceptorStack">
<property name="target">
<bean class="org.springframework.ejb.access.SimpleRemoteStatele ssSessionProxyFactoryBean">
<property name="businessInterface">
<value>mypackage.UserService</value>
</property>
<property name="jndiName">
<value>UserServiceBean-v1.6.0.0</value>
</property>
<property name="jndiTemplate">
<ref bean="jndiTemplate"/>
</property>
</bean>
</property>
</bean>
jdigger
Sep 16th, 2004, 07:16 AM
I still think not being able to proxy FactoryBeans is a problem, but the abstract template solution addresses my immediate problem quite well. Thanks!
jdigger
Sep 16th, 2004, 09:49 AM
btw: As written, that doesn't work -- CGLib complains about not being able to proxy a final class because it's trying to proxy a java.lang.Proxy using Proxy's interface. The solution is to specify the proxyInterfaces to the ProxyFactoryBean.
ie,
<bean id="userService" parent="serviceInterceptorStack">
<property name="proxyInterfaces">
<value>mypackage.UserService</value>
</property>
<property name="target">
<bean class="org.springframework.ejb.access.SimpleRemoteStatele ssSessionProxyFactoryBean">
<property name="businessInterface">
<value>mypackage.UserService</value>
</property>
<property name="jndiName">
<value>UserServiceBean-v1.6.0.0</value>
</property>
<property name="jndiTemplate">
<ref bean="jndiTemplate"/>
</property>
</bean>
</property>
</bean>
That kind of redundancy is annoying, so I overrode ProxyFactoryBean's setBeanFactory method with the following code:
if (getProxiedInterfaces().length == 0) {
final Class targetClass = getTargetSource().getTargetClass();
if (java.lang.reflect.Proxy.class.isAssignableFrom(ta rgetClass) ||
net.sf.cglib.proxy.Proxy.class.isAssignableFrom(ta rgetClass)) {
final Class[] classes = targetClass.getInterfaces();
for (int i = 0; i < classes.length; i++) {
addInterface(classes[i]);
}
}
}
that removes the need to specify the proxyInterfaces in the ProxyFactoryBean definition.
Rod Johnson
Sep 19th, 2004, 07:43 AM
I've added an issue in JIRA for this: http://opensource.atlassian.com/projects/spring/browse/SPR-337.
I'll try to look at it in the 1.1.2 timeframe. Not sure it's easy to address, but we'll see.
aidano
May 23rd, 2005, 08:32 PM
I've added an issue in JIRA for this: http://opensource.atlassian.com/projects/spring/browse/SPR-337.
I'll try to look at it in the 1.1.2 timeframe. Not sure it's easy to address, but we'll see.
Was this ever resolved? The link goes to a non-existent error (not sure if that's a good thing or not!)
hucmuc
May 24th, 2005, 12:58 PM
I've added an issue in JIRA for this: http://opensource.atlassian.com/projects/spring/browse/SPR-337.
I'll try to look at it in the 1.1.2 timeframe. Not sure it's easy to address, but we'll see.
Was this ever resolved? The link goes to a non-existent error (not sure if that's a good thing or not!)
Remove the dot (.) at the end of the jira link. The issue has not been addressed.
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.