共计 5498 个字符,预计需要花费 14 分钟才能阅读完成。
================= 进入 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));
}
所有配置源
创立 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<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
load(context, sources.toArray(new Object[0]));
listeners.contextLoaded(context);
}
================== postProcessApplicationContext(context);
protected void postProcessApplicationContext(ConfigurableApplicationContext context) {if (this.beanNameGenerator != null) {
// 注册组件
context.getBeanFactory().registerSingleton(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR,
this.beanNameGenerator);
}
if (this.resourceLoader != null) {
// 读取配置文件的资源
if (context instanceof GenericApplicationContext) {((GenericApplicationContext) context).setResourceLoader(this.resourceLoader);
}
if (context instanceof DefaultResourceLoader) {
// 资源加载器
((DefaultResourceLoader) context).setClassLoader(this.resourceLoader.getClassLoader());
}
}
// 筹备类型转换器
if (this.addConversionService) {context.getBeanFactory().setConversionService(ApplicationConversionService.getSharedInstance());
}
}
===================applyInitializers(context)
protected void applyInitializers(ConfigurableApplicationContext context) {
// 遍历所有的 ApplicationContextInitializer。调用 initialize。来对 ioc 容器进行初始化扩大性能
for (ApplicationContextInitializer initializer : getInitializers()) { // 之前从 spring.factories 找进去的七个 Initializer
Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(),
ApplicationContextInitializer.class);
Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
initializer.initialize(context);
}
}
@Override
public void initialize(ConfigurableApplicationContext context) {ConfigurableEnvironment environment = context.getEnvironment();
List<Class<?>> initializerClasses = getInitializerClasses(environment);
// 如果 initializerClasses 不为空,就把他们实例化。if (!initializerClasses.isEmpty()) {applyInitializerClasses(context, initializerClasses);
}
}
![上传中 …]()
正文完
发表至: springboot
2022-08-10