View Full Version : Pls Help: ProxyInterfaces V.S. ProxyTargetClass
smallfox
Mar 6th, 2005, 12:01 AM
Hi rod and all Spring experts,
I have encountered a problem that I don't know how to solve after reading through many posts here and Spring javadoc for a few hours.
Now i have defined an interface springtest.Bean, and a class springtest.BeanImpl implements the Bean interface.
In the following xml segment, if i uncomment proxyInterfaces and comment proxyTargetClass, the program works as expected. However, if i comment proxyInterfaces and uncomment proxyTargetClass, it gives me the following exception:
Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'myLovelyBean' defined in class path resource [aopbeans.xml]: Initialization of bean failed; nested exception is org.aopalliance.aop.AspectException: null
My understanding of the interception in Spring is that if it is an interface, Spring uses JDK dyn proxy while if it is a class, Spring delegates the job to CGLIB. So in either way, the program should work in the same way and the only difference is the underlying implementation of the proxied bean and mayby some runtime overhead. But it seems that Spring works differently, can anybody enlighten me on this? And probably give me an explanation why my "proxyTargetClass" doesnot work?
Thank you for your help in advanced!!!!
<bean id="beanTarget" class="springtest.BeanImpl"/>
<bean id="myLovelyBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- This works
<property name="proxyInterfaces"><value>springtest.Bean</value>
</property> -->
<!-- This won't work -->
<property name="proxyTargetClass"><value>true</value></property>
<property name="target"><ref local="beanTarget"/></property>
<property name="interceptorNames">
<list><value>foxlogging.advisor1</value></list>
</property>
</bean>
katentim
Mar 6th, 2005, 04:00 AM
I did a cut and paste - it worked on my system with my test interceptors (Spring 1.1.5). Can you post your interceptor configuration? Is there anymore stack trace?
dirk
Oct 13th, 2005, 06:37 AM
Hi
have you found a solution yet ? I tried the example described in
http://forum.springframework.org/showthread.php?t=15320 , but I get the following error:
[main] 13.10.05 13:12:31 1883 DEBUG DefaultListableBeanFactory - Bean 'paintColorAdvisor' instantiated via constructor [public org.springframework.aop.support.DefaultIntroductio nAdvisor(org.aopalliance.aop.Advice)]
[main] 13.10.05 13:12:31 1883 DEBUG DefaultListableBeanFactory - Invoking BeanPostProcessors before initialization of bean 'paintColorAdvisor'
[main] 13.10.05 13:12:31 1883 DEBUG DefaultListableBeanFactory - Invoking BeanPostProcessors after initialization of bean 'paintColorAdvisor'
[main] 13.10.05 13:12:31 1883 DEBUG ProxyFactoryBean - Adding advisor or TargetSource [DefaultIntroductionAdvisor: interfaces=[java.io.Serializable,org.springframework.aop.Intro ductionInfo,introductions.PaintColor]; introductionInterceptor=[introductions.PaintColorMixin@c3c315]] with name [paintColorAdvisor]
[main] 13.10.05 13:12:31 1893 DEBUG ProxyFactoryBean - Adding advisor with name [paintColorAdvisor]
[main] 13.10.05 13:12:31 1893 DEBUG ProxyFactoryBean - Added new aspect interface: java.io.Serializable
[main] 13.10.05 13:12:31 1893 DEBUG ProxyFactoryBean - Added new aspect interface: org.springframework.aop.IntroductionInfo
[main] 13.10.05 13:12:31 1893 DEBUG ProxyFactoryBean - Added new aspect interface: introductions.PaintColor
[main] 13.10.05 13:12:31 1893 DEBUG ProxyFactoryBean - Not refreshing target: bean name not specified in interceptorNames
[main] 13.10.05 13:12:31 1933 DEBUG Cglib2AopProxy - Creating CGLIB2 proxy for [introductions.Car]
[main] 13.10.05 13:12:31 2013 INFO DefaultListableBeanFactory - Destroying singletons in factory {org.springframework.beans.factory.support.Default ListableBeanFactory defining beans [carTarget,paintColorMixin,paintColorAdvisor,car]; root of BeanFactory hierarchy}
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'car' defined in class path resource [introductions/applicationContext.xml]: Initialization of bean failed; nested exception is org.aopalliance.aop.AspectException: null
Exception in thread "main"
I tried the following, but I never got it working :
- tried the newest release (1.2.5) and an older one (Spring 1.1.5) using the dependent jars distributed with spring
- tried the newest CGLIB release
- changed JVM from Java 1.3 to Java 1.4
- changed IBM JVM to Sun JVM
after some debugging I figured out that CGLIG cannot create the proxy for the class for some reason.
Is that a bug or am I doing something wrong (spring newbie) ?
robh
Oct 13th, 2005, 07:32 AM
I can't see any problem with the code, but if it persists can you post a reproducible test case to JIRA and I will look at this for the next release.
Rob
dirk
Oct 13th, 2005, 10:11 AM
I hope I can submit a test for that - I will try my best.
reduced to the minimum the following code causes the same (but more specific error) in my environment (Spring 1.2.5, Sun JDK 1.4) :
public static void main(String[] args) {
ProxyFactory pf = new ProxyFactory();
pf.setProxyTargetClass(true);
pf.addAdvice(new DebugInterceptor());
Object myTarget = new Object();
pf.setTarget(myTarget);
Object m = (Object) pf.getProxy();
}
produces the following :
main] 13.10.05 17:00:15 0 INFO DefaultAopProxyFactory - CGLIB2 available: proxyTargetClass feature enabled
[main] 13.10.05 17:00:15 30 INFO CollectionFactory - JDK 1.4+ collections available
[main] 13.10.05 17:00:15 100 INFO CollectionFactory - Commons Collections 3.x available
[main] 13.10.05 17:00:15 110 DEBUG CollectionFactory - Creating java.util.IdentityHashMap
[main] 13.10.05 17:00:19 3855 DEBUG Cglib2AopProxy - Creating CGLIB2 proxy for [java.lang.Object]
Exception in thread "main" java.lang.IllegalStateException: Unknown callback class org.springframework.aop.framework.Cglib2AopProxy$D ynamicAdvisedInterceptor
at net.sf.cglib.proxy.CallbackUtils.getGenerator(Call backUtils.java:102)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer. java:298)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:2 46)
at org.springframework.aop.framework.Cglib2AopProxy.g etProxy(Cglib2AopProxy.java:199)
at org.springframework.aop.framework.Cglib2AopProxy.g etProxy(Cglib2AopProxy.java:156)
at org.springframework.aop.framework.ProxyFactory.get Proxy(ProxyFactory.java:70)
at lp.app.Main.main(Main.java:35)
I have no idea !
robh
Oct 13th, 2005, 10:59 AM
Do you have multiple versions of either Spring, CGLIB or ASM on the classpath.
Rob
dirk
Oct 14th, 2005, 03:42 AM
Your are right !
I checked my classpath and found an old cglib-2.0-rc2.jar on my classpath before the jars that come with spring. (I accidentially confused my eclipse build-time classpath with the runtime configuration, which was different). Thank you very much, Rob!
For all others : spring.jar commons-logging.jar log4j-1.2.9.jar cglib-nodep-2.1_2.jar is sufficient to run the example I mentioned above (read readme.txt !). The error is sort of misleading because you dont get a NoClassDefFound message as usual.
robh
Oct 22nd, 2005, 07:28 AM
Indeed it is. The general problem here is that there are in fact two instances of the class found so a NoClassDefFoundError would be even more confusing.
In general if you ever see errors where it says something like 'Cannot assign com.myapp.MyClass to com.myapp.MyClass' or 'Invalid type com.myapp.MyClass' and you know the type is right then you probably have two instances of the class loaded.
Always remember that class X loaded by ClassLoader A is not the same as class X loaded by ClassLoader B.
Rob
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.