SpringBoot 创建容器的实现
作者:jiao个朋友 时间:2022-04-03 08:41:02
spring 容器的创建对应 SpringApplication 中 run 中调用的 createApplicationContext 方法。这里创建了一个 web 容器,接下就进去 prepareContext 容器准备阶段:
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,
SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
//为容器设置环境
context.setEnvironment(environment);
//这里的空实现留给开发者扩展,设置数据转换的ConversionService
postProcessApplicationContext(context);
//执行容器中的 Initializers 的 initialize 方法
applyInitializers(context);
listeners.contextPrepared(context);
if (this.logStartupInfo) {
logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context);
}
// Add boot specific singleton beans
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
if (printedBanner != null) {
beanFactory.registerSingleton("springBootBanner", printedBanner);
}
if (beanFactory instanceof DefaultListableBeanFactory) {
((DefaultListableBeanFactory) beanFactory)
.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
if (this.lazyInitialization) {
context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
}
// Load the sources
Set<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
load(context, sources.toArray(new Object[0]));
listeners.contextLoaded(context);
}
看一下这里的 load 方法,这里主要把我们的启动类作为 Bean 注册到了 Spring 的容器中。
protected void load(ApplicationContext context, Object[] sources) {
if (logger.isDebugEnabled()) {
logger.debug("Loading source " + StringUtils.arrayToCommaDelimitedString(sources));
}
BeanDefinitionLoader loader = createBeanDefinitionLoader(getBeanDefinitionRegistry(context), sources);
if (this.beanNameGenerator != null) {
loader.setBeanNameGenerator(this.beanNameGenerator);
}
if (this.resourceLoader != null) {
loader.setResourceLoader(this.resourceLoader);
}
if (this.environment != null) {
loader.setEnvironment(this.environment);
}
loader.load();
}
/**
* Load the sources into the reader.
* @return the number of loaded beans
*/
int load() {
int count = 0;
for (Object source : this.sources) {
count += load(source);
}
return count;
}
private int load(Object source) {
Assert.notNull(source, "Source must not be null");
if (source instanceof Class<?>) {
return load((Class<?>) source);
}
if (source instanceof Resource) {
return load((Resource) source);
}
if (source instanceof Package) {
return load((Package) source);
}
if (source instanceof CharSequence) {
return load((CharSequence) source);
}
throw new IllegalArgumentException("Invalid source type " + source.getClass());
}
private int load(Class<?> source) {
if (isGroovyPresent() && GroovyBeanDefinitionSource.class.isAssignableFrom(source)) {
// Any GroovyLoaders added in beans{} DSL can contribute beans here
GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class);
load(loader);
}
if (isEligible(source)) {
this.annotatedReader.register(source);
return 1;
}
return 0;
}
再来看下 contextLoaded 方法,这里将上下文设置到 * 中,同时也把 * 添加到上下文中。最后发布了一个 ApplicationPreparedEvent 事件。
public void contextLoaded(ConfigurableApplicationContext context) {
for (ApplicationListener<?> listener : this.application.getListeners()) {
if (listener instanceof ApplicationContextAware) {
((ApplicationContextAware) listener).setApplicationContext(context);
}
context.addApplicationListener(listener);
}
this.initialMulticaster.multicastEvent(new ApplicationPreparedEvent(this.application, this.args, context));
}
来源:https://segmentfault.com/a/1190000037447793
标签:SpringBoot,创建,容器
0
投稿
猜你喜欢
解析JavaSE的继承和多态
2023-11-24 16:13:48
详解java.lang.reflect.Modifier.isInterface()方法
2023-07-27 18:25:25
Spring MVC的优点与核心接口_动力节点Java学院整理
2023-11-28 05:43:36
spring boot metrics监控指标使用教程
2022-01-10 08:42:16
详解kafka中的消息分区分配算法
2021-06-02 08:16:15
java 生成xml并转为字符串的方法
2023-01-07 08:27:30
教你怎么使用Java实现WebSocket
2022-10-31 04:08:50
Spring 代理 Bean 获取不到原始 Bean 对象注解解决方法
2022-10-31 17:06:08
详解C#之事件
2022-01-07 22:18:14
Java NIO框架Netty简单使用的示例
2022-09-08 02:51:27
IDEA使用SequenceDiagram插件绘制时序图的方法
2023-07-03 11:20:07
C#解析json字符串总是多出双引号的原因分析及解决办法
2022-10-22 02:40:46
C#绘制时钟的方法
2021-08-25 15:18:12
C#使用foreach遍历哈希表(hashtable)的方法
2022-10-07 20:00:44
Java多线程之CAS算法实现线程安全
2022-12-09 17:53:53
C#后端接收form-data,创建实体类教程
2023-08-23 21:07:57
Spring如何处理注解的深入理解
2023-09-09 14:35:49
Spring Boot 读取静态资源文件的方法
2023-08-25 02:53:07
Spring源码解析之BeanPostProcessor知识总结
2022-04-07 22:13:34
C#二维数组与多维数组的具体使用
2023-07-30 23:53:49