spring的事务管理配置如下:
.....
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Service,*Manager</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="transactionInterceptor"/>
</bean>
......
程序在启动的时候,报错:
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'transactionManager': Bean with name 'transactionManager' has been
injected into other beans [transactionInterceptor] in its raw version as part of a circular reference, but has eventually been wrapped (for example as part of auto-
proxy creation). This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using
'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
出现了循环依赖的错误,终于发现是
<property name="beanNames">
<value>*Service,*Manager</value>
</property>
里面的问题,transactionManager后边也是Manager。这样会把transactionInterceptor注入到transactionManager中,故将bean id="transactionManager"改成其他的名字不用Manager结尾,或者将*Manager改成其他的就不会出现这个错误了。
如果要改bean transactionManager 的名字,记得将transactionInterceptor中<property name="transactionManager" ref="transactionManager"/>
的ref="transactionManager"也改成对应的名字。
网上还发现了一种解决方法,发现也可行,不求甚解,先记下:
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager" [color=red][b]lazy-init="true"[/b][/color] >
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
将bean id="transactionManager"延迟加载。
.....
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Service,*Manager</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="transactionInterceptor"/>
</bean>
......
程序在启动的时候,报错:
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'transactionManager': Bean with name 'transactionManager' has been
injected into other beans [transactionInterceptor] in its raw version as part of a circular reference, but has eventually been wrapped (for example as part of auto-
proxy creation). This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using
'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
出现了循环依赖的错误,终于发现是
<property name="beanNames">
<value>*Service,*Manager</value>
</property>
里面的问题,transactionManager后边也是Manager。这样会把transactionInterceptor注入到transactionManager中,故将bean id="transactionManager"改成其他的名字不用Manager结尾,或者将*Manager改成其他的就不会出现这个错误了。
如果要改bean transactionManager 的名字,记得将transactionInterceptor中<property name="transactionManager" ref="transactionManager"/>
的ref="transactionManager"也改成对应的名字。
网上还发现了一种解决方法,发现也可行,不求甚解,先记下:
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager" [color=red][b]lazy-init="true"[/b][/color] >
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
将bean id="transactionManager"延迟加载。