Announcement Announcement Module
Collapse
No announcement yet.
Declarative transaction with BeanNameAutoProxyCreator Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Declarative transaction with BeanNameAutoProxyCreator

    I have some trouble with BeanNameAutoProxyCreator.

    I have a lot of class with the same transactional methods. So i use BeanNameAutoProxyCreator to wrap them in a completely identical fashion.
    See:

    Code:
    <!-- define transaction interceptor -->
    	
    <bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    		<property name="transactionManager"><ref bean="transactionManager"/></property>
    		<property name="transactionAttributeSource"><ref bean="txAttributes"/></property>
    </bean>
    
    <!-- Define transactional methods &#40;NameMatchTransactionAttributeSource applies
    specific attributes to methods that match to a pattern&#41;  -->	
    	
    <bean id="txAttributes" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
    		<property name="properties"><value>add*=PROPAGATION_REQUIRED
    										   update*=PROPAGATION_REQUIRED
    										   delete*=PROPAGATION_REQUIRED
    									</value>
    		</property>
    </bean>
    
    
    <!-- Define transactional beans &#40;thanks Autoproxy&#41; i.e. every mappers -->
    	
    	<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    		<property name="interceptorNames"><value>txInterceptor</value></property>
    		<property name="beanNames"><value>*Mapper</value></property>
    	</bean>
    But when i inject one of these transactional beans in another bean:

    Code:
    <bean id="userRepository" class="com.pm.core.domain.user.UserRepositoryImpl"
              singleton="true" dependency-check="objects">
    	    <constructor-arg index="0"><ref bean="userMapper"/></constructor-arg>
        </bean>
    I have this exception :


    Error creating bean with name 'userRepository' defined in class path resource [coreContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy0] to required type [com.pm.core.data.user.UserMapper]
    Spring manuel says that "when BeanNameAutoProxyCreator postprocesses the target object and create the proxy, it causes the proxy to be inserted into the Application context under the name of the original bean"

    So how can i get the original bean and not his proxy ? And will transaction works ?

    thanks

    Arnaud

  • #2
    Is UserMapper a class or an interface? By default Spring will use dynamic proxies, which only proxy interfaces implemented by the target. You can also set the proxyTargetClass property to true to cause the usage of CGLIB, but are normally better to express dependencies on interfaces, not classes.

    Comment


    • #3
      UserMapper a class wich implements CRUD methods from a Mapper inteface.
      Im a a bit lost: how do I express dependencie on interface in this particiliar case ?

      thanks

      Arnaud

      Comment


      • #4
        Forget what i said i understand what you mean. In the userRepositoy class dependency was express on class not interface:

        Code:
        public UserRepositoryImpl&#40;UserMapper mapper&#41; &#123;
        		this.mapper = mapper;
        	&#125;
        instead of something like:

        Code:
        public UserRepositoryImpl&#40;UserMapperInterface mapper&#41; &#123;
        		this.mapper = mapper;
        	&#125;
        Thank you for your help ! Spring rocks!

        Arnaud

        Comment

        Working...
        X