spring ApplicationContext
程序员文章站
2022-05-25 12:37:41
...
文章目录
ApplicationContext 这个是带有上下文的容器
启动过程
以ClassPathXmlApplicationContext为例
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
// 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.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
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;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
contextRefresh.end();
}
}
1.prepareRefresh
在应用刷新前的一些准备工作
2.准备BeanFactory
生成beanfactory
DefaultListableBeanFactory beanFactory = createBeanFactory();
3.设置Beanfactory
prepareBeanFactory(beanFactory);
比如
// Configure the bean factory with context callbacks.
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
4.注册BeanFactoryPostProcessor
就是这个接口的实现
BeanFactoryPostProcessor
postProcessBeanFactory(beanFactory);
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
5.注册BeanPostProcessor
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
6.国际化处理
initMessageSource();
7.事件广播器
初始化事件广播器
initApplicationEventMulticaster();
8.注册listener
就是实现ApplicationListener这个接口的bean
监听事件得到通知
registerListeners();
9.实现化bean
finishBeanFactoryInitialization
10.结束
finishRefresh()
protected void finishRefresh() {
// Publish the final event.
发布这个事件,可以在前面实现监听这个事件
publishEvent(new ContextRefreshedEvent(this));
}
推荐阅读
-
SpringBoot 源码解析 (三)----- Spring Boot 精髓:启动时初始化数据
-
Spring-Cloud-Filter(过滤器)
-
spring依赖注入
-
bootstrap+spring mvc+ibatis 实现增删改查
-
gRPC在Spring Cloud中的应用
-
Spring 的 ApplicationEvent and ApplicationListener
-
Spring @Async异步执行方法
-
web.xml 配置 contextConfigLocation(多个Spring配置文件)
-
idea创建一个入门Spring Boot项目(controller层)使用Moven代码管理
-
Spring从MongoDB中下载文件之GridFS