- 1. ApplicationContextInitializer
- 1.1. ConfigurationWarningsApplicationContextInitializer
- 1.2. ContextIdApplicationContextInitializer
- 1.3. DelegatingApplicationContextInitializer
- 1.4. RSocketPortInfoApplicationContextInitializer
- 1.5. ServerPortInfoApplicationContextInitializer
- 1.6. SharedMetadataReaderFactoryContextInitializer
- 1.7. ConditionEvaluationReportLoggingListener
- 2. ApplicationListener
- 2.1. ClearCachesApplicationListener
- 2.2. ParentContextCloserApplicationListener
- 2.3. CloudFoundryVcapEnvironmentPostProcessor
- 2.4. FileEncodingApplicationListener
- 2.5. AnsiOutputApplicationListener
- 2.6. ConfigFileApplicationListener
- 2.7. DelegatingApplicationListener
- 2.8. ClasspathLoggingApplicationListener
- 2.9. LoggingApplicationListener
- 2.10. LiquibaseServiceLocatorApplicationListener
- 2.11. BackgroundPreinitializer
- 2.12. Initializer增加的
- 3. BeanDefinitionRegistryPostProcessor
- 4. ImportBeanDefinitionRegistrar
- 5. BeanFactoryPostProcessor
- 6. ImportSelector
- 7. BeanPostProcessor
- 8. FactoryBean
1. ApplicationContextInitializer
1.1. ConfigurationWarningsApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext context) {
context.addBeanFactoryPostProcessor(new ConfigurationWarningsPostProcessor(getChecks()));
}
1.2. ContextIdApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ContextId contextId = getContextId(applicationContext);
applicationContext.setId(contextId.getId());
applicationContext.getBeanFactory().registerSingleton(ContextId.class.getName(), contextId);
}
1.3. DelegatingApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext context) {
ConfigurableEnvironment environment = context.getEnvironment();
List<Class<?>> initializerClasses = getInitializerClasses(environment);
if (!initializerClasses.isEmpty()) {
applyInitializerClasses(context, initializerClasses);
}
}
1.4. RSocketPortInfoApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addApplicationListener(new Listener(applicationContext));
}
1.5. ServerPortInfoApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addApplicationListener(this);
}
1.6. SharedMetadataReaderFactoryContextInitializer
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addBeanFactoryPostProcessor(new CachingMetadataReaderFactoryPostProcessor());
}
1.7. ConditionEvaluationReportLoggingListener
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
this.applicationContext = applicationContext;
applicationContext.addApplicationListener(new ConditionEvaluationReportListener());
if (applicationContext instanceof GenericApplicationContext) {
// Get the report early in case the context fails to load
this.report = ConditionEvaluationReport.get(this.applicationContext.getBeanFactory());
}
}
2. ApplicationListener
2.1. ClearCachesApplicationListener
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ReflectionUtils.clearCache();
clearClassLoaderCaches(Thread.currentThread().getContextClassLoader());
}
2.2. ParentContextCloserApplicationListener
@Override
public void onApplicationEvent(ParentContextAvailableEvent event) {
maybeInstallListenerInParent(event.getApplicationContext());
}
2.3. CloudFoundryVcapEnvironmentPostProcessor
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
logger.switchTo(CloudFoundryVcapEnvironmentPostProcessor.class);
}
2.4. FileEncodingApplicationListener
2.5. AnsiOutputApplicationListener
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
String desired = environment.getProperty("spring.mandatory-file-encoding");
if (desired == null) {
return;
}
String encoding = System.getProperty("file.encoding");
if (encoding != null && !desired.equalsIgnoreCase(encoding)) {
if (logger.isErrorEnabled()) {
logger.error("System property 'file.encoding' is currently '" + encoding + "'. It should be '" + desired
+ "' (as defined in 'spring.mandatoryFileEncoding').");
logger.error("Environment variable LANG is '" + System.getenv("LANG")
+ "'. You could use a locale setting that matches encoding='" + desired + "'.");
logger.error("Environment variable LC_ALL is '" + System.getenv("LC_ALL")
+ "'. You could use a locale setting that matches encoding='" + desired + "'.");
}
throw new IllegalStateException("The Java Virtual Machine has not been configured to use the "
+ "desired default character encoding (" + desired + ").");
}
}
2.6. ConfigFileApplicationListener
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
}
if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent(event);
}
}
private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
List<EnvironmentPostProcessor> postProcessors = loadPostProcessors();
postProcessors.add(this);
AnnotationAwareOrderComparator.sort(postProcessors);
for (EnvironmentPostProcessor postProcessor : postProcessors) {
postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication());
}
}
List<EnvironmentPostProcessor> loadPostProcessors() {
//加载了并实例化了
//org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor
//org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor
//org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor
//org.springframework.boot.reactor.DebugAgentEnvironmentPostProcessor
return SpringFactoriesLoader.loadFactories(EnvironmentPostProcessor.class, getClass().getClassLoader());
}
2.7. DelegatingApplicationListener
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
List<ApplicationListener<ApplicationEvent>> delegates = getListeners(
((ApplicationEnvironmentPreparedEvent) event).getEnvironment());
if (delegates.isEmpty()) {
return;
}
this.multicaster = new SimpleApplicationEventMulticaster();
for (ApplicationListener<ApplicationEvent> listener : delegates) {
this.multicaster.addApplicationListener(listener);
}
}
if (this.multicaster != null) {
this.multicaster.multicastEvent(event);
}
}
2.8. ClasspathLoggingApplicationListener
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (logger.isDebugEnabled()) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
logger.debug("Application started with classpath: " + getClasspath());
}
else if (event instanceof ApplicationFailedEvent) {
logger.debug("Application failed to start with classpath: " + getClasspath());
}
}
}
2.9. LoggingApplicationListener
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationStartingEvent) {
onApplicationStartingEvent((ApplicationStartingEvent) event);
}
else if (event instanceof ApplicationEnvironmentPreparedEvent) {
onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
}
else if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent((ApplicationPreparedEvent) event);
}
else if (event instanceof ContextClosedEvent
&& ((ContextClosedEvent) event).getApplicationContext().getParent() == null) {
onContextClosedEvent();
}
else if (event instanceof ApplicationFailedEvent) {
onApplicationFailedEvent();
}
}
2.10. LiquibaseServiceLocatorApplicationListener
@Override
public void onApplicationEvent(ApplicationStartingEvent event) {
if (ClassUtils.isPresent("liquibase.servicelocator.CustomResolverServiceLocator",
event.getSpringApplication().getClassLoader())) {
new LiquibasePresent().replaceServiceLocator();
}
}
2.11. BackgroundPreinitializer
@Override
public void onApplicationEvent(SpringApplicationEvent event) {
if (!Boolean.getBoolean(IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME)
&& event instanceof ApplicationEnvironmentPreparedEvent && multipleProcessors()
&& preinitializationStarted.compareAndSet(false, true)) {
performPreinitialization();
}
if ((event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent)
&& preinitializationStarted.get()) {
try {
preinitializationComplete.await();
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
2.12. Initializer增加的
2.12.1 ServerPortInfoApplicationContextInitializer
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
String propertyName = "local." + getName(event.getApplicationContext()) + ".port";
setPortProperty(event.getApplicationContext(), propertyName, event.getWebServer().getPort());
}
3. BeanDefinitionRegistryPostProcessor
MapperScannerConfigurer.postProcessBeanDefinitionRegistry方法,用来扫描指定路径下的接口,注册为beanClass为MapperFactoryBean的BeanDefinition
4. ImportBeanDefinitionRegistrar
MapperScannerRegistrar,用来处理@MapperScan的basePackage
5. BeanFactoryPostProcessor
6. ImportSelector
7. BeanPostProcessor
8. FactoryBean
上一篇:SpringBoot
下一篇:事务管理