1. ApplicationContextInitializer

1.1. ConfigurationWarningsApplicationContextInitializer

  1. @Override
  2. public void initialize(ConfigurableApplicationContext context) {
  3. context.addBeanFactoryPostProcessor(new ConfigurationWarningsPostProcessor(getChecks()));
  4. }

1.2. ContextIdApplicationContextInitializer

  1. @Override
  2. public void initialize(ConfigurableApplicationContext applicationContext) {
  3. ContextId contextId = getContextId(applicationContext);
  4. applicationContext.setId(contextId.getId());
  5. applicationContext.getBeanFactory().registerSingleton(ContextId.class.getName(), contextId);
  6. }

1.3. DelegatingApplicationContextInitializer

  1. @Override
  2. public void initialize(ConfigurableApplicationContext context) {
  3. ConfigurableEnvironment environment = context.getEnvironment();
  4. List<Class<?>> initializerClasses = getInitializerClasses(environment);
  5. if (!initializerClasses.isEmpty()) {
  6. applyInitializerClasses(context, initializerClasses);
  7. }
  8. }

1.4. RSocketPortInfoApplicationContextInitializer

  1. @Override
  2. public void initialize(ConfigurableApplicationContext applicationContext) {
  3. applicationContext.addApplicationListener(new Listener(applicationContext));
  4. }

1.5. ServerPortInfoApplicationContextInitializer

  1. @Override
  2. public void initialize(ConfigurableApplicationContext applicationContext) {
  3. applicationContext.addApplicationListener(this);
  4. }

1.6. SharedMetadataReaderFactoryContextInitializer

  1. @Override
  2. public void initialize(ConfigurableApplicationContext applicationContext) {
  3. applicationContext.addBeanFactoryPostProcessor(new CachingMetadataReaderFactoryPostProcessor());
  4. }

1.7. ConditionEvaluationReportLoggingListener

  1. @Override
  2. public void initialize(ConfigurableApplicationContext applicationContext) {
  3. this.applicationContext = applicationContext;
  4. applicationContext.addApplicationListener(new ConditionEvaluationReportListener());
  5. if (applicationContext instanceof GenericApplicationContext) {
  6. // Get the report early in case the context fails to load
  7. this.report = ConditionEvaluationReport.get(this.applicationContext.getBeanFactory());
  8. }
  9. }

2. ApplicationListener

2.1. ClearCachesApplicationListener

  1. @Override
  2. public void onApplicationEvent(ContextRefreshedEvent event) {
  3. ReflectionUtils.clearCache();
  4. clearClassLoaderCaches(Thread.currentThread().getContextClassLoader());
  5. }

2.2. ParentContextCloserApplicationListener

  1. @Override
  2. public void onApplicationEvent(ParentContextAvailableEvent event) {
  3. maybeInstallListenerInParent(event.getApplicationContext());
  4. }

2.3. CloudFoundryVcapEnvironmentPostProcessor

  1. @Override
  2. public void onApplicationEvent(ApplicationPreparedEvent event) {
  3. logger.switchTo(CloudFoundryVcapEnvironmentPostProcessor.class);
  4. }

2.4. FileEncodingApplicationListener

2.5. AnsiOutputApplicationListener

  1. @Override
  2. public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
  3. ConfigurableEnvironment environment = event.getEnvironment();
  4. String desired = environment.getProperty("spring.mandatory-file-encoding");
  5. if (desired == null) {
  6. return;
  7. }
  8. String encoding = System.getProperty("file.encoding");
  9. if (encoding != null && !desired.equalsIgnoreCase(encoding)) {
  10. if (logger.isErrorEnabled()) {
  11. logger.error("System property 'file.encoding' is currently '" + encoding + "'. It should be '" + desired
  12. + "' (as defined in 'spring.mandatoryFileEncoding').");
  13. logger.error("Environment variable LANG is '" + System.getenv("LANG")
  14. + "'. You could use a locale setting that matches encoding='" + desired + "'.");
  15. logger.error("Environment variable LC_ALL is '" + System.getenv("LC_ALL")
  16. + "'. You could use a locale setting that matches encoding='" + desired + "'.");
  17. }
  18. throw new IllegalStateException("The Java Virtual Machine has not been configured to use the "
  19. + "desired default character encoding (" + desired + ").");
  20. }
  21. }

2.6. ConfigFileApplicationListener

  1. @Override
  2. public void onApplicationEvent(ApplicationEvent event) {
  3. if (event instanceof ApplicationEnvironmentPreparedEvent) {
  4. onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
  5. }
  6. if (event instanceof ApplicationPreparedEvent) {
  7. onApplicationPreparedEvent(event);
  8. }
  9. }
  10. private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
  11. List<EnvironmentPostProcessor> postProcessors = loadPostProcessors();
  12. postProcessors.add(this);
  13. AnnotationAwareOrderComparator.sort(postProcessors);
  14. for (EnvironmentPostProcessor postProcessor : postProcessors) {
  15. postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication());
  16. }
  17. }
  18. List<EnvironmentPostProcessor> loadPostProcessors() {
  19. //加载了并实例化了
  20. //org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor
  21. //org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor
  22. //org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor
  23. //org.springframework.boot.reactor.DebugAgentEnvironmentPostProcessor
  24. return SpringFactoriesLoader.loadFactories(EnvironmentPostProcessor.class, getClass().getClassLoader());
  25. }

2.7. DelegatingApplicationListener

  1. @Override
  2. public void onApplicationEvent(ApplicationEvent event) {
  3. if (event instanceof ApplicationEnvironmentPreparedEvent) {
  4. List<ApplicationListener<ApplicationEvent>> delegates = getListeners(
  5. ((ApplicationEnvironmentPreparedEvent) event).getEnvironment());
  6. if (delegates.isEmpty()) {
  7. return;
  8. }
  9. this.multicaster = new SimpleApplicationEventMulticaster();
  10. for (ApplicationListener<ApplicationEvent> listener : delegates) {
  11. this.multicaster.addApplicationListener(listener);
  12. }
  13. }
  14. if (this.multicaster != null) {
  15. this.multicaster.multicastEvent(event);
  16. }
  17. }

2.8. ClasspathLoggingApplicationListener

  1. @Override
  2. public void onApplicationEvent(ApplicationEvent event) {
  3. if (logger.isDebugEnabled()) {
  4. if (event instanceof ApplicationEnvironmentPreparedEvent) {
  5. logger.debug("Application started with classpath: " + getClasspath());
  6. }
  7. else if (event instanceof ApplicationFailedEvent) {
  8. logger.debug("Application failed to start with classpath: " + getClasspath());
  9. }
  10. }
  11. }

2.9. LoggingApplicationListener

  1. @Override
  2. public void onApplicationEvent(ApplicationEvent event) {
  3. if (event instanceof ApplicationStartingEvent) {
  4. onApplicationStartingEvent((ApplicationStartingEvent) event);
  5. }
  6. else if (event instanceof ApplicationEnvironmentPreparedEvent) {
  7. onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
  8. }
  9. else if (event instanceof ApplicationPreparedEvent) {
  10. onApplicationPreparedEvent((ApplicationPreparedEvent) event);
  11. }
  12. else if (event instanceof ContextClosedEvent
  13. && ((ContextClosedEvent) event).getApplicationContext().getParent() == null) {
  14. onContextClosedEvent();
  15. }
  16. else if (event instanceof ApplicationFailedEvent) {
  17. onApplicationFailedEvent();
  18. }
  19. }

2.10. LiquibaseServiceLocatorApplicationListener

  1. @Override
  2. public void onApplicationEvent(ApplicationStartingEvent event) {
  3. if (ClassUtils.isPresent("liquibase.servicelocator.CustomResolverServiceLocator",
  4. event.getSpringApplication().getClassLoader())) {
  5. new LiquibasePresent().replaceServiceLocator();
  6. }
  7. }

2.11. BackgroundPreinitializer

  1. @Override
  2. public void onApplicationEvent(SpringApplicationEvent event) {
  3. if (!Boolean.getBoolean(IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME)
  4. && event instanceof ApplicationEnvironmentPreparedEvent && multipleProcessors()
  5. && preinitializationStarted.compareAndSet(false, true)) {
  6. performPreinitialization();
  7. }
  8. if ((event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent)
  9. && preinitializationStarted.get()) {
  10. try {
  11. preinitializationComplete.await();
  12. }
  13. catch (InterruptedException ex) {
  14. Thread.currentThread().interrupt();
  15. }
  16. }
  17. }

2.12. Initializer增加的

2.12.1 ServerPortInfoApplicationContextInitializer

  1. @Override
  2. public void onApplicationEvent(WebServerInitializedEvent event) {
  3. String propertyName = "local." + getName(event.getApplicationContext()) + ".port";
  4. setPortProperty(event.getApplicationContext(), propertyName, event.getWebServer().getPort());
  5. }

3. BeanDefinitionRegistryPostProcessor

MapperScannerConfigurer.postProcessBeanDefinitionRegistry方法,用来扫描指定路径下的接口,注册为beanClass为MapperFactoryBean的BeanDefinition

4. ImportBeanDefinitionRegistrar

MapperScannerRegistrar,用来处理@MapperScan的basePackage

5. BeanFactoryPostProcessor

6. ImportSelector

7. BeanPostProcessor

8. FactoryBean