您的位置:首页 > 编程语言 > Java开发

spring bean初始化过程

2017-06-26 17:56 387 查看
SpringBean 初始化过程
ClassPathXmlApplication reflsh()

//记录执行时间
prepareRefresh();

//主要是创建beanFactory(DefaultListableBeanFactory),同时加载配置文件.xml中的beanDefinition
//通过String[] configLocations = getConfigLocations()获取资源路径,
//加载beanDefinition(XmlBeanDefinitionReader.loadBeanDefinitions(configLocations))
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

//准备beanFactory
//设置默认的组件(classLoader)
prepareBeanFactory(beanFactory);

try {
//提供给子类实现一些postProcess的注册,如AbstractRefreshableWebApplicationContext注册一些Servlet相关的
//postProcess,真对web进行生命周期管理的Scope,通过registerResolvableDependency()方法注册指定ServletRequest,
//HttpSession,WebRequest对象的工厂方法
postProcessBeanFactory(beanFactory);

// Invoke factory processors registered as beans in the context.
//调用所有BeanFactoryProcessor的postProcessBeanFactory()方法
invokeBeanFactoryPostProcessors(beanFactory);

// Register bean processors that intercept bean creation.
//注册BeanPostProcessor(BeanPostProcessor作用是用于拦截Bean的创建)
registerBeanPostProcessors(beanFactory);

// Initialize message source for this context.
//初始化消息Bean
initMessageSource();

// Initialize event multicaster for this context.
//初始化上下文的事件传播组建,ApplicationEvent触发时由multicaster通知给ApplicationListener
initApplicationEventMulticaster();

// Initialize other special beans in specific context subclasses.
//ApplicationContext初始化一些特殊的bean
onRefresh();

// Check for listener beans and register them.
///注册事件监听器,事件监听Bean统一注册到multicaster里头,ApplicationEvent事件触发后会由multicaster广播
registerListeners();

// Instantiate all remaining (non-lazy-init) singletons.
//非延迟加载的单例Bean实例化
finishBeanFactoryInitialization(beanFactory);

// Last step: publish corresponding event.
finishRefresh();
}

catch (BeansException ex) {
// Destroy already created singletons to avoid dangling resources.
destroyBeans();

// Reset 'active' flag.
cancelRefresh(ex);

// Propagate exception to caller.
throw ex;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: