随笔-124  评论-194  文章-0  trackbacks-0
有时候,在SPRING中两个类互相含有对方的声明,一般情况这是不允许并且极可能是有错误的。
会报这个错:
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘***’: Bean with name ‘***’ has been injected into other beans [***, ***]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.


但有时候这正是我们想要的,考虑这种情况:
我们用一个ManagerFactory来包含所有Manager的声明,以便在程序中用到多个Manager的地方,一个入口集中访问。但是,可能某个Manager本身就需要用到其它几个Manager,进而用到ManagerFactory。这时,我们可以这样声明即可:

 <bean id="managerFactory" class="common.utils.ManagerFactory" lazy-init="true"/>
posted on 2008-02-24 10:13 我爱佳娃 阅读(37793) 评论(11)  编辑  收藏 所属分类: Spring

评论:
# re: SPRING循环依赖(circular reference)的解决方法 2008-02-24 10:48 | 完美世界私服
<bean id="managerFactory" class="common.utils.ManagerFactory" lazy-init="true"/>
???????  回复  更多评论
  
# re: SPRING循环依赖(circular reference)的解决方法 2008-02-24 20:39 | 我爱佳娃
即在循环依赖的BEAN定义中,加入属性值:lazy-init="true",只需有一方A是延迟初始化,另一边B即不会因为A又指向自己而出现上述错误。  回复  更多评论
  
# re: SPRING循环依赖(circular reference)的解决方法[未登录] 2008-07-12 02:39 | james
你测试过吗,你的spring版本是多少?
在2.0.8下,你即使加了lazy-init="true",也不行  回复  更多评论
  
# re: SPRING循环依赖(circular reference)的解决方法 2008-07-13 11:41 | 我爱佳娃
我所举例子,即我的程序代码,一直工作很好。
MAVEN的SPRING配置片断:
<spring.version>2.5.1</spring.version>

<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>concurrent</groupId>
<artifactId>concurrent</artifactId>
<version>1.3.4</version>
</dependency>
<!-- spring end -->  回复  更多评论
  
# re: SPRING循环依赖(circular reference)的解决方法[未登录] 2008-11-20 17:21 | yang
我的spring2.5,加上 lazy-init="true" 还是错误依旧。  回复  更多评论
  
# re: SPRING循环依赖(circular reference)的解决方法 2008-11-22 08:27 | 我爱佳娃
我也没啥好建议了!不过可以尝试:
请使用2.5.1版本。
另外,只在一边加lazy-init,两边换着试下。  回复  更多评论
  
# re: SPRING循环依赖(circular reference)的解决方法 2008-11-27 11:27 | fofo
@我爱佳娃
但如果A中有B,B中有A的话,如何延迟一方都是不成功的!这个参数lazy-init是不是只适合于一方依赖一方,互相依赖的话应该不行吧!有没有其他解决方案呢?  回复  更多评论
  
# re: SPRING循环依赖(circular reference)的解决方法 2009-01-15 07:57 | 鲲鹏鸿翔
好用~!  回复  更多评论
  
# re: SPRING循环依赖(circular reference)的解决方法[未登录] 2009-10-21 10:27 | test
# re: SPRING循环依赖(circular reference)的解决方法 2013-11-15 10:46 | mingzai
业务类spring配置时候不要用prototype,用单例模式.  回复  更多评论
  
# re: SPRING循环依赖(circular reference)的解决方法 2014-11-25 16:08 | liabco
谢谢,加上 lazy-init="true"问题解决了   回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: