Spring对加载的bean之间循环依赖的处理
根据下面文档的叙述,简言之:
- 对于相互之间通过构造函数注入相互循环依赖的情况,Spring会抛出BeanCurrentlyInCreationException错误。
- 如果AB两个beans是通过属性注入的循环依赖,spring会通过先对其中一个bean赋值之后再对另外一个bean赋值,来打破循环依赖。因此不会有问题。
Circular dependencies
If you use predominantly constructor injection, it is possible to create an unresolvable circular dependency scenario.
For example: Class A requires an instance of class B through constructor injection, and class B requires an instance of class A through constructor injection. If you configure beans for classes A and B to be injected into each other, the Spring IoC container detects this circular reference at runtime, and throws a >BeanCurrentlyInCreationException.
One possible solution is to edit the source code of some classes to be configured by setters rather than constructors. Alternatively, avoid constructor injection and use setter injection only. In other words, although it is not recommended, you can configure circular dependencies with setter injection.
Unlike the typical case (with no circular dependencies), a circular dependency between bean A and bean B forces one of the beans to be injected into the other prior to being fully initialized itself (a classic chicken/egg scenario).
最新文章
- 常用SQL[ORACLE]
- LoadRunner之篇
- CoolTrayIcon4.0
- 每一个程序员需要了解的10个Linux命令
- invalid byte 1 of 1-byte UTF-8 sequence
- android 分段显示文本颜色控件
- 经典SQL语句大全_主外键_约束
- CSS3 基本知识
- ASP.NET MVC 自我总结的便捷开发实例
- 洛谷 P1219 八皇后【经典DFS,温习搜索】
- python爬虫之基本知识
- vue.js 视频播放
- C#中的函数式编程:递归与纯函数(二)
- ruby写一个文件内容相似性比较的代码
- COMCMS v0.9 版本发布,带前后端的一个响应式企业站
- 使用Hexo搭建Github静态博客
- Java 线程池 Executor 框架
- CAN设计与应用指南
- django 中的render和render_to_response()和locals()
- STL__网上资料
热门文章
- php对象引用和析构函数的关系
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
- 猪八戒吃西瓜(wmelon)-排序-查找
- 【python】时间性能分析
- Ubuntu12.04安装ia32-libs
- August 1st, 2016, Week 32nd Monday
- 解决 jersey javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
- PA
- HTML CSS 中DIV内容居中汇总
- ytu 1938:首字母变大写(水题)