Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Autoproxying

  1. #1
    Join Date
    Apr 2007
    Location
    Adrogué, Buenos Aires, Argentina
    Posts
    34

    Default Autoproxying

    Hi, I have a problem with autoproxying. Can I use autoproxying and manual proxying (by xml) on the same beans? Where can I find more information than in the reference about how does autoproxying work?
    My problem is that when I try to autoproxy a proxyed bean, an exception is thrown.
    Code:
    17:02:19,187 - ERROR - [] - [org.springframework.web.context.ContextLoader] - Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMapping' defined in class path resource [Spring-ServiceMappings.xml]: Can't resolve reference to bean 'urlMappingsFactoryBean' while setting property 'urlMap'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMappingsFactoryBean': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entidadesBasicas.domain.MapeoServicioInstitucionCRUDService' defined in class path resource [Spring-EntidadesBasicasServiceProxies.xml]: Initialization of bean failed; nested exception is org.aopalliance.aop.AspectException: null
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMappingsFactoryBean': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entidadesBasicas.domain.MapeoServicioInstitucionCRUDService' defined in class path resource [Spring-EntidadesBasicasServiceProxies.xml]: Initialization of bean failed; nested exception is org.aopalliance.aop.AspectException: null
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entidadesBasicas.domain.MapeoServicioInstitucionCRUDService' defined in class path resource [Spring-EntidadesBasicasServiceProxies.xml]: Initialization of bean failed; nested exception is org.aopalliance.aop.AspectException: null
    java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy49
    Thanks!!!!

    Leandro

  2. #2
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Proxying a proxy can be problematic. In your case it even seems to be impossible. Your manual proxy seems to be a CGLib one, not one based on JDK dynamic proxies. From what I understand it's final and not further on sub classable. Isn't it possible to apply multiple advisors in one proxying step? Both chapter 6 and chapter 7 are about AOP, 7.9 in particular is about autoproxying.

    Jörg

  3. #3
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Is it possible to see what you are actually trying to do in the configuration?
    Last edited by karldmoore; Aug 30th, 2007 at 05:40 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  4. #4
    Join Date
    Apr 2007
    Location
    Adrogué, Buenos Aires, Argentina
    Posts
    34

    Default

    Yes, this is the autoproxy configuration:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    	<bean id="logAutoProxyCreator"
    		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    		<property name="beanNames">
    			<value>*ServiceTarget</value>
    		</property>
    		<property name="interceptorNames">
    			<idref bean="logAdvice" />
    		</property>
    		<property name="proxyTargetClass">
    			<value>true</value>
    		</property>
    	</bean>
    </beans>
    I've added the proxyTargetClass property and now the exception is:

    Code:
    09:28:10,140 - ERROR - [] - [org.springframework.web.context.ContextLoader] - Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMapping' defined in class path resource [Spring-ServiceMappings.xml]: Can't resolve reference to bean 'urlMappingsFactoryBean' while setting property 'urlMap'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMappingsFactoryBean': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginService' defined in class path resource [Spring-SeguridadServiceProxies.xml]: Can't resolve reference to bean 'loginServiceTarget' while setting property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginServiceTarget' defined in class path resource [Spring-SeguridadServiceImpls.xml]: Initialization of bean failed; nested exception is org.aopalliance.aop.AspectException: null
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMappingsFactoryBean': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginService' defined in class path resource [Spring-SeguridadServiceProxies.xml]: Can't resolve reference to bean 'loginServiceTarget' while setting property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginServiceTarget' defined in class path resource [Spring-SeguridadServiceImpls.xml]: Initialization of bean failed; nested exception is org.aopalliance.aop.AspectException: null
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginService' defined in class path resource [Spring-SeguridadServiceProxies.xml]: Can't resolve reference to bean 'loginServiceTarget' while setting property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginServiceTarget' defined in class path resource [Spring-SeguridadServiceImpls.xml]: Initialization of bean failed; nested exception is org.aopalliance.aop.AspectException: null
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginServiceTarget' defined in class path resource [Spring-SeguridadServiceImpls.xml]: Initialization of bean failed; nested exception is org.aopalliance.aop.AspectException: null
    java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
    	at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhancer.java:718)
    	at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:499)
    	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:199)
    	at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:156)
    	at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:70)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:375)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:237)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:267)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:364)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:226)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:147)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:176)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:105)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1013)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:824)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:345)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:226)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:147)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getType(AbstractBeanFactory.java:342)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.isBeanTypeMatch(DefaultListableBeanFactory.java:249)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:144)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:198)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:192)
    	at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:608)
    And this is the manual configuration on loginServiceTarget:
    Code:
    	<bean id="loginServiceTarget"
    		class="seguridad.service.LoginServiceImpl">
    		<constructor-arg>
    			<ref bean="hibernateRepository" />
    		</constructor-arg>
    		<property name="novellLDAPDAO">
    			<ref bean="novellLDAPDAO" />
    		</property>
    		<property name="loginPolicyValidator">
    			<ref bean="loginPolicyValidator" />
    		</property>
    	</bean>
    and loginService:
    Code:
    	<bean id="loginService"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="target">
    			<ref bean="loginServiceTarget" />
    		</property>
    		<property name="transactionManager">
    			<ref bean="transactionManager" />
    		</property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="*">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    		<property name="preInterceptors">
    			<list>
    				<ref bean="logAdvisor" />
    				<ref bean="exceptionAdvisor" />
    			</list>
    		</property>
    		<property name="postInterceptors">
    			<list>
    				<ref bean="soloLogEventAdvisor" />
    			</list>
    		</property>
    	</bean>
    Thanks!!

    Leandro

  5. #5
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Why can't you add the "logAdvice" to either the TransactionProxyFactoryBean's interceptors instead of using the autowiring?
    Last edited by karldmoore; Aug 30th, 2007 at 05:40 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  6. #6
    Join Date
    Apr 2007
    Location
    Adrogué, Buenos Aires, Argentina
    Posts
    34

    Default

    Because I want to add the logAdvice to all my services.

    Leandro

  7. #7
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I would guess that you are using JDK dynamic proxies for the TransactionProxyFactoryBean and as I understand it, these are final. Have you tried proxyTargetClass="true" on this as well?
    http://java.sun.com/j2se/1.5.0/docs/...ion/proxy.html
    Last edited by karldmoore; Aug 30th, 2007 at 05:40 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  8. #8
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by karldmoore View Post
    I would guess that you are using JDK dynamic proxies for the TransactionProxyFactoryBean and as I understand it, these are final. Have you tried proxyTargetClass="true" on this as well?
    Actually it must be the other way around. CGLib proxies need a default constructor which the class obviously does not have:

    Code:
    java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
    	at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhancer.java:718)
    	at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:499)
    	at net.sf.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
    (Also the stacktrace points to CGLib, Karl )

    So he needs to switch to JDK dynamic proxies - or provide a default constructor.

    Jörg

  9. #9
    Join Date
    Apr 2007
    Location
    Adrogué, Buenos Aires, Argentina
    Posts
    34

    Default

    Yes, the problem was the lack of the default constructor...
    But now I've a new one...
    Code:
    09:02:06,234 - ERROR - [] - [org.springframework.web.context.ContextLoader] - Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMapping' defined in class path resource [Spring-ServiceMappings.xml]: Can't resolve reference to bean 'urlMappingsFactoryBean' while setting property 'urlMap'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMappingsFactoryBean': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mapeoServicioInstitucionServiceImpl' defined in class path resource [Spring-EntidadesBasicasServiceProxies.xml]: Can't resolve reference to bean 'mapeoServicioInstitucionServiceImplTarget' while setting property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mapeoServicioInstitucionServiceImplTarget' defined in class path resource [Spring-EntidadesBasicasServiceImpls.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyAccessExceptionsException: PropertyAccessExceptionsException (1 errors); nested propertyAccessExceptions are: [org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy29] to required type [services.crud.AbstractCRUDService] for property 'servicioInstitucionCRUDService']
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMappingsFactoryBean': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mapeoServicioInstitucionServiceImpl' defined in class path resource [Spring-EntidadesBasicasServiceProxies.xml]: Can't resolve reference to bean 'mapeoServicioInstitucionServiceImplTarget' while setting property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mapeoServicioInstitucionServiceImplTarget' defined in class path resource [Spring-EntidadesBasicasServiceImpls.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyAccessExceptionsException: PropertyAccessExceptionsException (1 errors); nested propertyAccessExceptions are: [org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy29] to required type [services.crud.AbstractCRUDService] for property 'servicioInstitucionCRUDService']
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mapeoServicioInstitucionServiceImpl' defined in class path resource [Spring-EntidadesBasicasServiceProxies.xml]: Can't resolve reference to bean 'mapeoServicioInstitucionServiceImplTarget' while setting property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mapeoServicioInstitucionServiceImplTarget' defined in class path resource [Spring-EntidadesBasicasServiceImpls.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyAccessExceptionsException: PropertyAccessExceptionsException (1 errors); nested propertyAccessExceptions are: [org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy29] to required type [services.crud.AbstractCRUDService] for property 'servicioInstitucionCRUDService']
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mapeoServicioInstitucionServiceImplTarget' defined in class path resource [Spring-EntidadesBasicasServiceImpls.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyAccessExceptionsException: PropertyAccessExceptionsException (1 errors); nested propertyAccessExceptions are: [org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy29] to required type [services.crud.AbstractCRUDService] for property 'servicioInstitucionCRUDService']
    PropertyAccessExceptionsException (1 errors)
    org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy29] to required type [services.crud.AbstractCRUDService] for property 'servicioInstitucionCRUDService'
    	at org.springframework.beans.BeanWrapperImpl.doTypeConversionIfNecessary(BeanWrapperImpl.java:839)
    	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:584)
    	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:469)
    	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:626)
    	at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:653)
    	at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:642)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1023)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:824)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:345)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:226)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:147)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:176)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:105)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1013)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:824)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:345)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:226)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:147)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getType(AbstractBeanFactory.java:342)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.isBeanTypeMatch(DefaultListableBeanFactory.java:249)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:144)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:198)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:192)
    	at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:608)
    This is the new autoproxy configuration:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    	<bean id="logAutoProxyCreator"
    		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    		<property name="beanNames">
    			<value>*ServiceTarget, *ServiceImplTarget</value>
    		</property>
    		<property name="interceptorNames">
    			<idref bean="logAdvice" />
    		</property>
    		<property name="proxyTargetClass">
    			<value>true</value>
    		</property>
    	</bean>
    </beans>
    This the target configuration:
    Code:
    	<bean id="mapeoServicioInstitucionServiceImplTarget" class="entidadesBasicas.services.impl.MapeoServicioInstitucionServiceImpl">
    		<property name="mapeoServicioInstitucionCRUDService">
    			<ref bean="entidadesBasicas.domain.MapeoServicioInstitucionCRUDService" />
    		</property>
    		<property name="aperturaProgramaticaCRUDService">
    			<ref bean="entidadesBasicas.domain.AperturaProgramaticaCRUDService" />
    		</property>
    		<property name="imputacionFisicaCRUDService">
    			<ref bean="entidadesBasicas.domain.ImputacionFisicaCRUDService" />
    		</property>
    		<property name="servicioInstitucionCRUDService">
    			<ref bean="tablasBasicas.ElementoBasicoCRUDService" />
    		</property>
    	</bean>
    and this is the proxy configuration:
    Code:
    	<bean id="mapeoServicioInstitucionServiceImpl"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="target">
    			<ref bean="mapeoServicioInstitucionServiceImplTarget" />
    		</property>
    		<property name="transactionManager">
    			<ref bean="transactionManager" />
    		</property>
    		<property name="preInterceptors">
    			<list>
    				<ref bean="logAdvisor" />
    				<ref bean="exceptionAdvisor" />
    				<ref bean="dataAccessExceptionInterceptor" />
    			</list>
    		</property>
    		<property name="postInterceptors">
    			<list>
    				<ref
    bean="entidadesBasicasBusinessServiceValidationAdvisor" />
    			</list>
    		</property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="mapear">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
    Do you know how can I resolve it?
    Thanks!!!!!!!

    Leandro

  10. #10
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Now you are at the point having JDK dynamic proxies where you actually need CGLib proxies since you don't program to interfaces. As Karl described it (actually he is ahead of the times, he did see this problem coming! ) you have to add the property proxyTargetClass="true" to TransactionProxyFactoryBean.

    But I guess you will run into another problem since you are proxying your object using the BeanNameAutoProxyCreator and the TransactionProxyFactoryBean. So you end up with twice proxied objects. It really should be avoided if it works at all.

    (From a recent thread I got that CGLib proxies are actually final classes and can't be extended. Since they are themselves based on extending the original class your setup would fail. I haven't confirmed that myself though.)

    Jörg
    Last edited by Jörg Heinicke; Jun 13th, 2007 at 11:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •