當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring源码解析-实例化bean对象
生活随笔
收集整理的這篇文章主要介紹了
Spring源码解析-实例化bean对象
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
spring加載配置文件,AbstractApplicationContext類中的refresh方法起著重要的作用。
@Overridepublic void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing.準備刷新context prepareRefresh();// Tell the subclass to refresh the internal bean factory.刷新子類beanFactory,注冊beanDefinitionConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context.準備beanfactory prepareBeanFactory(beanFactory);try {// Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation. registerBeanPostProcessors(beanFactory);// Initialize message source for this context. initMessageSource();// Initialize event multicaster for this context. initApplicationEventMulticaster();// Initialize other special beans in specific context subclasses. onRefresh();// Check for listener beans and register them. registerListeners();// Instantiate all remaining (non-lazy-init) singletons.初始化非懶加載的bean實例 finishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event. finishRefresh();}catch (BeansException ex) {logger.warn("Exception encountered during context initialization - cancelling refresh attempt", ex);// Destroy already created singletons to avoid dangling resources. destroyBeans();// Reset 'active' flag. cancelRefresh(ex);// Propagate exception to caller.throw ex;}}}實例化bean對象,主要的是在
finishBeanFactoryInitialization(beanFactory)?方法。
大致的流程圖(下部分):
?
(點擊查看大圖)
下面分析實例化bean對象的源碼分析
加載bean對象:
?
在AbstractBeanFactory類中,根據不同scope進行實例化,例如Singleton,Prototype等
?
?
?
?
?
?
根據不同情況采用不同的代理方式:
到這里就是bean實例的初始化,可以看到spring采用的代理方式,來實現bean的初始化。
?
轉載于:https://www.cnblogs.com/lzeffort/p/7669150.html
總結
以上是生活随笔為你收集整理的Spring源码解析-实例化bean对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 山姆会员商店如何办会员卡(山姆超市必买清
- 下一篇: 每天十分钟系列:JS数据操作之神奇的ma