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
Java 查找Springbean之间循环依赖关系的良好实践_Java_Spring_Circular Dependency - Fatal编程技术网

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依赖关系图”

IntelliJ给了我一个非常好的依赖关系图,尽管我不得不增加Xmx的大小,以便为操作提供足够的内存。右键单击应用程序上下文文件Diagrams>Dependency diagram。有时加载需要一段时间。如果基于注释的应用程序上下文不起作用,请尝试将组件扫描移动到application.xml,然后重试。

对我有效的一种方法是使用一个主方法编写一个助手类,该方法实例化可刷新的应用程序上下文,不允许循环依赖项并调用刷新。确保bean/app上下文配置文件位于类路径上

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。