Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring singleton创建、销毁、再次创建_Spring - Fatal编程技术网

Spring singleton创建、销毁、再次创建

Spring singleton创建、销毁、再次创建,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' wh

我在春季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被销毁

稍后,通过另一个getType()调用再次创建我的bean。t此时间春天没有任何循环路径问题

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

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

编辑:


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

不会创建和销毁单例。它在第一次创建尝试时失败,因为它依赖于尚未创建的
B
,并且由于某些原因,无法在
A
第一次请求时创建它。因此,Spring试图第二次成功地创建它。不管怎么说,你发帖的方式似乎很难猜测到底发生了什么。如何将
B
注入
A
?@XtremeBiker,在两个不同的对象上调用init()方法两次,其中第二个对象保留在上下文中。这怎么不是“创造两次”?我没有验证是否调用了destroy方法(我还没有),但我确实看到调用了destroySingleton,如果我有destroy方法的话,应该调用destroy方法。也许我没有解释清楚,对象当然正在创建中,但没有分配给Spring上下文,我的意思是。这就是Spring第二次尝试的原因。
BeanA将BeanB作为构造函数参数
。你可以试着用一个塞特来注射它。@XtremeBiker,试过-相同的区别:(这是另一个难题。