Announcement Announcement Module
Collapse
No announcement yet.
Pls Help: ProxyInterfaces V.S. ProxyTargetClass Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pls Help: ProxyInterfaces V.S. ProxyTargetClass

    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:

    Code:
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: 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!!!!


    Code:
      <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>

  • #2
    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?

    Comment


    • #3
      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:

      Code:
      [main] 13.10.05 13:12:31 1883  DEBUG DefaultListableBeanFactory - Bean 'paintColorAdvisor' instantiated via constructor [public org.springframework.aop.support.DefaultIntroductionAdvisor(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.IntroductionInfo,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.DefaultListableBeanFactory defining beans [carTarget,paintColorMixin,paintColorAdvisor,car]; root of BeanFactory hierarchy} 
      org.springframework.beans.factory.BeanCreationException: 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) ?
      Last edited by robyn; May 14th, 2006, 06:11 PM.

      Comment


      • #4
        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

        Comment


        • #5
          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) :

          Code:
          		
          public static void main&#40;String&#91;&#93; args&#41; &#123;
                                          ProxyFactory pf = new ProxyFactory&#40;&#41;;
          		pf.setProxyTargetClass&#40;true&#41;;
           		pf.addAdvice&#40;new DebugInterceptor&#40;&#41;&#41;;
           		Object myTarget = new Object&#40;&#41;;
          		pf.setTarget&#40;myTarget&#41;;
          		Object m = &#40;Object&#41; pf.getProxy&#40;&#41;;
             &#125;
          produces the following :

          Code:
          main&#93; 13.10.05 17&#58;00&#58;15 0     INFO  DefaultAopProxyFactory - CGLIB2 available&#58; proxyTargetClass feature enabled 
          &#91;main&#93; 13.10.05 17&#58;00&#58;15 30    INFO  CollectionFactory - JDK 1.4+ collections available 
          &#91;main&#93; 13.10.05 17&#58;00&#58;15 100   INFO  CollectionFactory - Commons Collections 3.x available 
          &#91;main&#93; 13.10.05 17&#58;00&#58;15 110   DEBUG CollectionFactory - Creating java.util.IdentityHashMap 
          &#91;main&#93; 13.10.05 17&#58;00&#58;19 3855  DEBUG Cglib2AopProxy - Creating CGLIB2 proxy for &#91;java.lang.Object&#93; 
          Exception in thread "main" java.lang.IllegalStateException&#58; Unknown callback class org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor
          	at net.sf.cglib.proxy.CallbackUtils.getGenerator&#40;CallbackUtils.java&#58;102&#41;
          	at net.sf.cglib.proxy.Enhancer.createHelper&#40;Enhancer.java&#58;298&#41;
          	at net.sf.cglib.proxy.Enhancer.create&#40;Enhancer.java&#58;246&#41;
          	at org.springframework.aop.framework.Cglib2AopProxy.getProxy&#40;Cglib2AopProxy.java&#58;199&#41;
          	at org.springframework.aop.framework.Cglib2AopProxy.getProxy&#40;Cglib2AopProxy.java&#58;156&#41;
          	at org.springframework.aop.framework.ProxyFactory.getProxy&#40;ProxyFactory.java&#58;70&#41;
          	at lp.app.Main.main&#40;Main.java&#58;35&#41;
          I have no idea !

          Comment


          • #6
            Do you have multiple versions of either Spring, CGLIB or ASM on the classpath.

            Rob

            Comment


            • #7
              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.

              Comment


              • #8
                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

                Comment

                Working...
                X