SpringBoot 创建容器的实现
作者:jiao个朋友 发布时间:2022-04-03 08:41:02
标签:SpringBoot,创建,容器
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


猜你喜欢
- 本文实例讲述了java中response对象用法。分享给大家供大家参考,具体如下:<jsp:forward>动作元素用于运行时在
- 给新建的winform程序添加资源文件夹Resources小菜鸟开始学习WinForm程序别人的项目都有资源文件夹放图片之类的,我的就是没有
- 在android studio中存储数据有三个方法,分别是:(1)简单存储——SharedPreferences(2)文件存储:内部存储——
- 这个应该是简易版的美图秀秀(小伙伴们吐槽:你这也叫简易版的??我们看着怎么不像啊……)。好吧,只是在图片上绘制涂鸦,然后保存。一、选择图片这
- google benchmark已经为我们提供了类似的功能,而且使用相当简单。具体的解释在后面,我们先来看几个例子,我们人为制造几个时间复杂
- java 实现截取字符串并按字节分别输出实例代码前言:请编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要
- 这里主要介绍的是优先队列的二叉堆Java实现,代码如下:package practice;import edu.princeton.cs.a
- 目录了解程序集如何在C#.NET中加载程序集,模块和引用.NET中的程序集绑定绑定重定向当问题开始发生时故障排除边注References了解
- 在使用线程池的时候,发现除了execute()方法可以执行任务外,还发现有一个方法submit()可以执行任务。submit()有3个参数不
- 本文主要介绍了spring-boot-maven-plugin报红解决方案,亲测有效,具体如下:<?xml version="
- 1.基本介绍SpringBoot 支持的 webServer: Tomcat, Jetty, or UndertowSpringBoot 应
- android客户端生成本地验证码主要用来限制用户随意按请求按钮,其实该示例也是来对自定义view的练练手而已,先给出效果图吧其中可定制:*
- 在最近的项目中有个需求是这样的:入参封装成JSON,EXAMPLE:{ "uuid": "iamauuid&q
- Spring AOP proxyTargetClass的行为要点列表形式proxyTargetClasstrue目标对象实现了接口 – 使用
- Usage xml android:background= ?attr/zzbackground app:backgroundAttr= z
- springboot集成mybatis plus和dynamic-datasource注意事项环境spring-boot-starter-p
- 使用@Value取值出现的问题在springBoot项目中我们一般会把一些路径或者资源写在配置文件中,方便管理。但是取得时候有可能会出现一些
- 引言从本篇文章开始,我们将介绍 Java AQS 的实现方式,本文先介绍 AQS 的内部数据是如何组织的,后面的文章中再分别介绍 AQS 的
- Mybatis批量插入index out of range错误往往我们看到网上关于各类关于批量插入报这种错误的文章都是传入的集合为null,
- 本文实例讲述了Android SQLite数据库操作方法。分享给大家供大家参考,具体如下:SQLite and AndroidSQLite简