了解Spring上下文初始化顺序 [英] Understanding Spring context initialization order

查看:109
本文介绍了了解Spring上下文初始化顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一套复杂的bean和它们之间的依赖关系.所有bean均带有@Service@Repository@Controller注释,我使用了@PostConstruct注释.有一些循环依赖关系,但仍然可以通过Spring正确地初始化系统.

I have a complex set of beans and dependencies between them. All beans are @Service, @Repository or @Controller annotated and I use the @PostConstruct annotation. There are some circular dependencies but still the system was correctly initialized by Spring.

然后,我添加了一个仅具有对服务之一的依赖项的简单Controller.从理论上讲,该系统应该能够启动,因为从理论上讲它可以像以前一样先设置系统,然后再安装新的Controller.但是Spring抱怨说它无法设置上下文:

Then I added a simple Controller with just a dependency to one of the Services. Theoretically, the system should be able to boot because theoretically it could first set up the system as before and then the new Controller. But Spring complains that it cannot set up the context:

Error creating bean with name 'userService': Requested bean is currently in creation: Is there an unresolvable circular reference?

我可以以某种方式协助Spring如何安排上下文初始化吗?我认为主要的问题是userService,它在系统中经常用于身份验证.

Can I somehow assists Spring in how to order the context initialization? I think the main issue is the userService which is used a lot through the system for authentication purposes.

推荐答案

最好的解决方案就是删除循环依赖项.我还没有遇到过这样的结构是必要的情况.但是,如果您确实想坚持下去,那么上面的问题可能是由于您在某处有构造函数注入而造成的:

Best solution would simply be to take out the circular dependency; I have not yet encountered a scenario where such a structure was warranted. But if you do want to stick with it then perhaps your above problem is due to the fact that you have constructor injection somewhere:

循环依赖 如果主要使用构造函数注入,则可以编写和配置您的类和bean,从而创建无法解决的循环依赖方案. 考虑以下场景:您有一个类A,它要求通过构造函数注入提供类B的实例,而一个类B,它需要通过构造函数注入提供类A的实例.如果为将类A和B相互注入而配置了bean,则Spring IoC容器将在运行时检测到此循环引用,并抛出BeanCurrentlyInCreationException.

Circular dependencies If you are using predominantly constructor injection it is possible to write and configure your classes and beans such that an unresolvable circular dependency scenario is created. Consider the scenario where you have class A, which requires an instance of class B to be provided via constructor injection, and class B, which requires an instance of class A to be provided via constructor injection. If you configure beans for classes A and B to be injected into each other, the Spring IoC container will detect this circular reference at runtime, and throw a BeanCurrentlyInCreationException.

对此问题的一种可能的解决方案是 编辑其中一些的源代码 您的班级将通过配置 setter,而不是通过构造函数. 另一个解决方案是不使用 构造函数注入并坚持 仅限二传手注射.换一种说法, 尽管通常应避免使用,但最罕见的是 情况,有可能 使用配置循环依赖 二传手注射.不像典型的 情况(无循环依赖性), Bean A之间的循环依赖 而B豆将迫使其中一个B豆 在注入之前 本身已完全初始化(a 经典的鸡/蛋场景).

One possible solution to this issue is to edit the source code of some of your classes to be configured via setters instead of via constructors. Another solution is not to use constructor injection and stick to setter injection only. In other words, while it should generally be avoided in all but the rarest of circumstances, it is possible to configure circular dependencies with setter injection. Unlike the typical case (with no circular dependencies), a circular dependency between bean A and bean B will force one of the beans to be injected into the other prior to being fully initialized itself (a classic chicken/egg scenario).

这篇关于了解Spring上下文初始化顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆