/** List of bean definition names, in registration order */ List<String> beanDefinitionNames /** Map from bean name to merged RootBeanDefinition */ Map<String, RootBeanDefinition> mergedBeanDefinitions
/** 保存單例對象: bean名 --> bean實例 */ private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256);
Tags:(BeanFactory具體對應的子類是DefaultListableBeanFactory)segmentfault
沒有則緩存
參考百度腦圖: [調用beanFactory.getBean獲取/建立實例]節點3d
在上下文刷新時AbstractApplicationContext#refreshcode
// 執行徹底部的BeanFactory後置處理 // ConfigurationClassPostProcessor會加載工程下的class到BeanDefinition // 註冊Bean後置處理器 // 根據BeanDefinition實例化全部單例Bean. finishBeanFactoryInitialization(beanFactory);
getBean方法經過Bean後置處理器AutowiredAnnotationBeanPostProcessor注入依賴, 該後置處理器又將調用getBean獲取依賴Bean, 例若有以下代碼:對象
@Service public class ServiceA { @Autowired ServiceB serviceB; } @Service public class ServiceB { @Autowired ServiceA serviceA; }
BeanFactory會將建立中的bean名保存到singletonsCurrentlyInCreation, 建立中的bean實例保存到singletonFactories。token
被依賴的Bean會先完成初始化
參考圖片連接: Spring循環引用邏輯結構圖圖片
DefaultSingletonBeanRegistry對象的singletonObjects字段, 這裏註冊的都是依賴已經注入, 且執行完Bean後置處理器的實例ip
/** Cache of singleton objects: bean name --> bean instance */ private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256);
BeanCreationException: Circular depends-on relationship between
循環依賴會致使該異常, 如使用@DependsOn註解配置了依賴:get
@Service @DependsOn("permission") public class Role { @Autowired private Permission permission; } @Service @DependsOn("role") public class Permission { @Autowired private Role role; }
Permission,Role互相依賴, 誰沒法被建立, BeanFactory將拋出異常BeanCreationException:
Circular depends-on relationship between 'role' and 'permission'
it