=================进入getOrCreateEnvironment()
private ConfigurableEnvironment getOrCreateEnvironment() {
if (this.environment != null) {
return this.environment;
}
switch (this.webApplicationType) {
case SERVLET: //如果是原生servlet编程
return new StandardServletEnvironment();
case REACTIVE: //如果是响应式编程
return new StandardReactiveWebEnvironment();
default:
return new StandardEnvironment();
}
}====================进入configureEnvironment()
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
//给环境添加一些类型转换器
if (this.addConversionService) {
ConversionService conversionService = ApplicationConversionService.getSharedInstance();
environment.setConversionService((ConfigurableConversionService) conversionService);
}
//加载(读取)外部配置源
//即读取所有的配置源的配置属性值
configurePropertySources(environment, args);
//激活Profiles环境
configureProfiles(environment, args);
}
==========进入configurePropertySources()
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
//获取到所有配置源
MutablePropertySources sources = environment.getPropertySources();
DefaultPropertiesPropertySource.ifNotEmpty(this.defaultProperties, sources::addLast);
//命令行信息
if (this.addCommandLineProperties && args.length > 0) {
String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
if (sources.contains(name)) {
PropertySource> source = sources.get(name);
CompositePropertySource composite = new CompositePropertySource(name);
composite.addPropertySource(
new SimpleCommandLinePropertySource("springApplicationCommandLineArgs", args));
composite.addPropertySource(source);
sources.replace(name, composite);
}
else {
sources.addFirst(new SimpleCommandLinePropertySource(args));
}
}
}==============进入environmentPrepared()
void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
doWithListeners("spring.boot.application.environment-prepared",
(listener) -> listener.environmentPrepared(bootstrapContext, environment));
}
所有配置源
文章图片
【8.2、SpringBoot启动流程--把spring应用跑起来(接8.1)】创建IOC容器 createApplicationContext()
protected ConfigurableApplicationContext createApplicationContext() {
return this.applicationContextFactory.create(this.webApplicationType);
}
=============根据当前的应用类型(Servlet)创建容器
//当前会创建AnnotationConfigServletWebServerApplicationContext
ApplicationContextFactory DEFAULT = (webApplicationType) -> {
try {
switch (webApplicationType) {
case SERVLET:
return new AnnotationConfigServletWebServerApplicationContext();
case REACTIVE:
return new AnnotationConfigReactiveWebServerApplicationContext();
default:
return new AnnotationConfigApplicationContext();
}
}
catch (Exception ex) {
throw new IllegalStateException("Unable create a default ApplicationContext instance, "
+ "you may need a custom ApplicationContextFactory", ex);
}
};
准备ApplicationContext IOC容器的基本信息 prepareContext()
private void prepareContext(DefaultBootstrapContext bootstrapContext, ConfigurableApplicationContext context,
ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments, Banner printedBanner) {
//保存前面的基础环境
context.setEnvironment(environment);
//IOC容器的后置处理流程。
postProcessApplicationContext(context);
//应用初始化器:applyInitializers
applyInitializers(context);
listeners.contextPrepared(context);
bootstrapContext.close(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
![上传中...]()
推荐阅读
- 8.1、SpringBoot启动流程--把spring应用跑起来
- java|零基础微信完整版小程序开发之微信表情包小程序前端+后台源码(java)
- springboot|Spring Security跨站请求伪造(CSRF)
- flowable|spring cloud alibaba gateway nacos 503错误代码
- SpringBoot+Uniapp实战开发全新仿抖音短视频App某课内置资料
- java|springboot 整合redis集群
- SpringBoot|SpringBoot 根据 注解和切面(AOP) 实时验证用户登陆状态
- springBoot|自定义拦截器实现权限校验
- springBoot|springBoot使用拦截器实现权限验证,解决注入为空