标签:creates lis factory oid configure load [] tsp create
在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
spring-boot-autoconfigure-1.5.2.RELEASE.jar
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的作用分析
标签:creates lis factory oid configure load [] tsp create
原文地址:http://www.cnblogs.com/hzhuxin/p/7742365.html