zoukankan      html  css  js  c++  java
  • Spring对加载的bean之间循环依赖的处理

    根据下面文档的叙述,简言之:

    1. 对于相互之间通过构造函数注入相互循环依赖的情况,Spring会抛出BeanCurrentlyInCreationException错误。
    2. 如果AB两个beans是通过属性注入的循环依赖(Singleton),spring会通过先对其中一个bean赋值之后再对另外一个bean赋值,来打破循环依赖。因此不会有问题。实际上是用默认构造函数初始化A之后,放入earlyBeanFactory,使之可以被获取,然后实现循环依赖的处理。
      3.对于prototype的bean,spring无法处理循环依赖,因为spring不缓存prototype的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).

  • 相关阅读:
    gentoo 网络配置
    gentoo 安装
    渗透流程与相应的工具使用
    Web 安全工具篇:Burp Suite 使用指南
    kali&BT5下利用nmap对mysql等数据库渗透与爆破
    一个网站的渗透测试思路,流程
    网站渗透测试原理及详细过程
    mysql渗透过程
    AWVS介绍
    Windows下Python3.6.2+Django-1.11.5+httpd-2.4.27-win64-VC14部署网站
  • 原文地址:https://www.cnblogs.com/miniwiki/p/6181657.html
Copyright © 2011-2022 走看看