Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Transaction - Proxy confusion

 <tx:advice id="txAdvice" transaction-manager="jtaTxManager">
  <tx:attributes>
   <tx:method name="*" />
  </tx:attributes>
 </tx:advice>

 <aop:config proxy-target-class="true">
  <aop:pointcut id="fooServiceOperation"
   expression="execution(* x.y.SampClass.save(..))" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
 </aop:config>

 <bean id="Samp1" class=" x.y.SampClass"></bean>

        <bean id="SearchDispatchRpcGwtServlet" class="x.y.server.SearchDispatchRpcGwtServlet">
  <constructor-arg>
       <list>
         <ref bean="webServiceClient"/>                          
       </list>
  </constructor-arg>      
 </bean>

 <!-- Service Clients --> 
 <bean id="webServiceClient" class="x.y.KSBClientProxyFactoryBean">
  <property name="serviceEndpointInterface" value="x.y.service.WebService" />
  <property name="serviceQName" value="{http://x.y.org/wsdl/organization}WebService" />
 </bean>

This is the part of sample spring context file. I am trying to create a transaction advice for SampClass to execute on the save method.

So from my understanding it should create a proxy for SampClass only.

I have a SearchDispatchRpcGwtServlet which takes as an argument a webservice Client which is also a proxy in itself. This bean is also getting proxied for some reason where it fails because it cannot create a proxy of a proxy.

I must add that SearchDispatchRpcGwtServlet creates an instance of SampClass and calls the save method.

I get the following exception:

java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy118 at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer .java:446) at net.sf.cglib.transform.TransformingClassGenerator. generateClass(TransformingClassGenerator.java:33)

I dont understand why SearchDispatchRpcGwtServlet proxy is getting created. Can someone explain.

Addition Stack Trace

Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class $Proxy117]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy117
    at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:213)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:473)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:348)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:309)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:361)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:1427)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport$1.run(FactoryBeanRegistrySupport.java:139)
    ... 85 more
Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy117
    at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
    at net.sf.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
    at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:201)
like image 996
nagl Avatar asked Feb 10 '10 15:02

nagl


1 Answers

a subclass of AbstractAutoProxyCreator is trying to proxy a bean, but that bean is already itself a CGLIB proxy created using another mechanism such as aop:config proxy-target-class="true.

To avoid this, make sure to avoid mixing the different aspect weaving mechanisms (ProxyBeanFactory and aop:config ) for the same bean.

In this case the transaction aspects can also be weaved via <tx:annotation:driven/> via the @Transactional annotation.

Alternatively it would be possible to remove the autowiring/scan from the bean and do the injection of dependencies via XML using setters.

Another alternative is to use load time weaving everywhere by declaring <context:load-time-weaver/> and adding the needed jars.

Have a look also at this post, it general it would be better to use only one one way of applying the aspects in the whole application, in order to avoid this problem.

like image 104
Angular University Avatar answered Sep 24 '22 15:09

Angular University