Results 1 to 4 of 4

Thread: Declarative transaction with BeanNameAutoProxyCreator

  1. #1
    Join Date
    Sep 2004
    Location
    Lyon, France
    Posts
    10

    Default 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. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    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.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Sep 2004
    Location
    Lyon, France
    Posts
    10

    Default

    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

  4. #4
    Join Date
    Sep 2004
    Location
    Lyon, France
    Posts
    10

    Default

    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

Similar Threads

  1. Unit testing with JOTM and JtaTransactionManager
    By lalle in forum Architecture
    Replies: 1
    Last Post: Oct 15th, 2005, 09:05 AM
  2. Replies: 0
    Last Post: Jun 6th, 2005, 06:22 AM
  3. Replies: 6
    Last Post: May 17th, 2005, 11:38 PM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Transaction pb Hibernate/MySQL
    By syluser in forum Data
    Replies: 2
    Last Post: Aug 28th, 2004, 02:40 PM

Posting Permissions

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