Java 查找Springbean之间循环依赖关系的良好实践
我有一个例外:Java 查找Springbean之间循环依赖关系的良好实践,java,spring,circular-dependency,Java,Spring,Circular Dependency,我有一个例外: SEVERE: Context initialization failedorg.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'myService': Bean with name 'myService' has been injected into other beans [otherService] in its raw versio
SEVERE: Context initialization failedorg.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'myService': Bean with name 'myService' has been injected into other beans [otherService] in its raw version as part of a circular reference, but has eventually been wrapped. 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.
有谁能提出好的策略来寻找循环依赖的来源吗
我目前正在浏览上下文定义,但正如您可能想象的,在一个成熟的项目中,这需要相当长的时间
因此,我一般都在寻找快速查找循环bean依赖关系的方法。这里有两个工具可以宣传依赖关系图的生成。然而,我对他们没有任何经验
- -它宣传“SpringBean依赖关系图”
- -表示“bean依赖关系图”
public class ContextChecker {
public static void main( String[] args ) {
AbstractRefreshableApplicationContext ctx =
new ClassPathXmlApplicationContext( "classpath*:/beans.xml" );
ctx.setAllowCircularReferences( false );
ctx.refresh();
}
}
此类可以作为Java应用程序运行,例如从IDE内部运行。如果在您的上下文中检测到循环依赖项,则错误消息将提供有关违规bean的信息。我知道OP的情况有所不同,但供将来参考: 如果您有此异常:
SEVERE: Context initialization failedorg.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'myService': Bean with name 'myService' has been injected into other beans [otherService] in its raw version as part of a circular reference, but has eventually been wrapped. 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.
- org.springframework.beans.factory.beancurrentlyIncremerationException:创建名为“a”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用
- org.springframework.beans.factory.unsatifiedDependencyException:创建文件[filename]中定义的名为“a”的bean时出错:通过索引为[com.gk.simple.B]的构造函数参数表示的未满足的依赖项:创建文件[filename]中定义的名为“B”的bean时出错:通过具有[com.gk.simple.A]类型的索引0的构造函数参数表示的未满足的依赖关系:创建名为“A”的bean时出错。:当前正在创建请求的bean:是否存在无法解析的循环引用
这里的循环是a->b->a。您的配置是否使用XML、注释或配置类?下面是一篇关于解决问题的好文章:错误消息不是试图说bean
otherService
有问题吗?是否有某种方式使得otherService
依赖于myService
,而myService
也依赖于otherService
?你是在问如何查看bean链接以便检测依赖关系如何循环吗?@Gray不,它们彼此不依赖。不是直接的,这就是我想要找到的。它可能类似于otherService->3rdService->myservice和myservice->3rdService->otherService。我最终会通过挖掘上下文找到它,但我想知道是否有人知道一种快速/可靠的方法。如果您使用其中一种方法,请在可以的情况下添加对其工作原理的评论。祝你好运。感谢您的检查。eclipse的bean依赖关系图很好,它绘制了非常有用的漂亮图形。您只需要将spring项目性质添加到您的一个项目中,切换到SpringEclipse视图,右键单击您的项目,然后单击其中一个bean上的DrawDependencyGraph。