1. 程式人生 > >Spring Boot中的initializers的作用分析

Spring Boot中的initializers的作用分析

ack array init pes rtl ble set bsp warn

在SpringApplication的實例屬性中有一個初始器的屬性:List<ApplicationContextInitializer<?>> initializers ,這些初始化器(initializers)是Spring Boot通過讀取每個jar包下的/META-INF/spring.factories文件中的配置獲取的。每一個initailizer都是一個實現了ApplicationContextInitializer接口的實例。ApplicationContextInitializer是Spring IOC容器中提供的一個接口:

package org.springframework.context;

public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> { /** * Initialize the given application context. * @param applicationContext the application to configure */ void initialize(C applicationContext); }
ApplicationContextInitializer是一個回調接口,它會在ConfigurableApplicationContext的refresh()方法調用之前被調用,做一些容器的初始化工作。

SpringBoot默認情況下提供了6個initializer,分別由2個jar提供:

 spring-boot-1.5.2.RELEASE.jar

  •   org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,
  •   org.springframework.boot.context.ContextIdApplicationContextInitializer,
  •   org.springframework.boot.context.config.DelegatingApplicationContextInitializer,
  •   org.springframework.boot.context.embedded.ServerPortInfoApplicationContextInitializer

spring-boot-autoconfigure-1.5.2.RELEASE.jar

  •   org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,
  •   org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer

Spring Boot對initializer的獲取過程如下:

initialize(Object[] sources)
  -->getSpringFactoriesInstances(type, new Class<?>[] {})
      -->SpringFactoriesLoader.loadFactoryNames(type, classLoader))
      -->createSpringFactoriesInstances(type, parameterTypes,classLoader, args, names)
        -->Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes)
        -->T instance = (T) BeanUtils.instantiateClass(constructor, args)
      -->AnnotationAwareOrderComparator.sort(instances)

-->setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class ))

   -->this.initializers = new ArrayList<ApplicationContextInitializer<?>>();

   -->this.initializers.addAll(initializers); //存入List<ApplicationContextInitializer<?>> initializers

Spring Boot中的initializers的作用分析