首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring 循环依赖(circular reference)有关问题

2012-10-08 
Spring 循环依赖(circular reference)问题spring的事务管理配置如下:.....!-- 事务管理 --bean idtra

Spring 循环依赖(circular reference)问题
spring的事务管理配置如下:
.....

<!-- 事务管理 -->
<bean id="transactionManager"
/>
</property>
</bean>

<bean id="transactionInterceptor"
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
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"
/>
</property>
</bean>

将bean id="transactionManager"延迟加载。 1 楼 javaanddonet 2011-04-14   请问:
楼主在服务层中注入的时候,采用的是构造方法注入还是Setter方法注入呢?

热点排行