Spring单例创建,销毁,再次创建

时间:2016-04-12 13:42:58

标签: spring

我在Spring 3.2.5中看到了一种特殊的行为

我的一个bean是由AbstractBeanFactory#getType

创建的

然后Spring按照

的方式抛出异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'A' defined in class path resource [config.xml]: Cannot resolve reference to bean 'B' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'B': Requested bean is currently in creation: Is there an unresolvable circular reference?

由于该异常,我的bean被销毁了。

稍后,我的bean又从另一个getType()调用中创建。这个时间春天没有任何循环路径问题。

最终,整个上下文成功创建,我的bean的第二个副本保留为单例。

我的问题是 - 这种行为正常吗?或者我做错了什么?我的bean在其init()方法中有副作用,所以如果这是Spring的正常行为,我需要向它添加一个destroy()方法......

编辑:

澄清有关异常的问题:异常中提到的bean并不完全相关,因为上下文(包括bean A和B)最终成功创建,因此确实没有循环依赖。但是,为了完整起见,bean A将bean B作为构造函数参数。

0 个答案:

没有答案
相关问题