当Spring容器实例化之后,此时需要针对容器进行一些初始化的操作。比如环境属性、父子容器关系、加载一些最初的资源类等。
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,
SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
// 1. 填充环境属性
context.setEnvironment(environment);
// 2. 后置处理 添加一些属性 最重要的就是添加conversionService
postProcessApplicationContext(context);
// 3. ApplicationContextInitializer扩展
applyInitializers(context);
// 4. 发布ApplicationContextInitializedEvent事件
listeners.contextPrepared(context);
if (this.logStartupInfo) {
// 5. 答应启动日志信息
logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context);
}
// Add boot specific singleton beans
// 6. 手动添加一些单例bean springApplicationArguments和springBootBanner
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
if (printedBanner != null) {
beanFactory.registerSingleton("springBootBanner", printedBanner);
}
// 7. 设置允许bean定义覆盖
if (beanFactory instanceof DefaultListableBeanFactory) {
((DefaultListableBeanFactory) beanFactory)
.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
// Load the sources
// 8. 加载所有资源
Set<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
// 9. 根据资源进行类定义的注册 此处就包括主类的注册
load(context, sources.toArray(new Object[0]));
// 10 发布ApplicationPreparedEvent事件
listeners.contextLoaded(context);
}
填充环境属性
此时已经读取了默认配置文件的信息
@Override
public void setEnvironment(ConfigurableEnvironment environment) {
super.setEnvironment(environment);
// 此处会重新设置ConditionEvaluator,因为后面会根据配置文件进行bean是否注入的判断
this.reader.setEnvironment(environment);
this.scanner.setEnvironment(environment);
}
扩展点ApplicationContextInitializer
- 通过DelegatingApplicationContextInitializer可以在刷新容器之前进行一些扩展,比如添加BeanFactoryPostProcessor。
/**
* Apply any {@link ApplicationContextInitializer}s to the context before it is
* refreshed.
* @param context the configured ApplicationContext (not refreshed yet)
* @see ConfigurableApplicationContext#refresh()
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void applyInitializers(ConfigurableApplicationContext context) {
// 在构造SpringApplication通过SPI读取的initializers
for (ApplicationContextInitializer initializer : getInitializers()) {
Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(),
ApplicationContextInitializer.class);
Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
initializer.initialize(context);
}
}
public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> {
/**
* Initialize the given application context.
* @param applicationContext the application to configure
*/
void initialize(C applicationContext);
}
参考源码
// org.springframework.boot.context.config.DelegatingApplicationContextInitializer
// ApplicationContextInitializer that delegates to other initializers that are specified under a context.initializer.classes environment property.
private static final String PROPERTY_NAME = "context.initializer.classes";
@Override
public void initialize(ConfigurableApplicationContext context) {
ConfigurableEnvironment environment = context.getEnvironment();
List<Class<?>> initializerClasses = getInitializerClasses(environment);
if (!initializerClasses.isEmpty()) {
applyInitializerClasses(context, initializerClasses);
}
}
private List<Class<?>> getInitializerClasses(ConfigurableEnvironment env) {
// 通过参数进行配置
String classNames = env.getProperty(PROPERTY_NAME);
List<Class<?>> classes = new ArrayList<>();
if (StringUtils.hasLength(classNames)) {
for (String className : StringUtils.tokenizeToStringArray(classNames, ",")) {
classes.add(getInitializerClass(className));
}
}
return classes;
}
比如在application.properties配置文件添加如下配置:
context.initializer.classes=com.example.managingtransactions.MyApplicationContextInitializer
对应类为
package com.example.managingtransactions;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
public class MyApplicationContextInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
System.out.println(applicationContext.getBeanFactory().getBeanDefinitionNames());
}
}
2. SharedMetadataReaderFactoryContextInitializer添加bean工厂后置处理器
public static final String BEAN_NAME = "org.springframework.boot.autoconfigure."
+ "internalCachingMetadataReaderFactory";
/**
* {@link BeanDefinitionRegistryPostProcessor} to register the
* {@link CachingMetadataReaderFactory} and configure the
* {@link ConfigurationClassPostProcessor}.
*/
private static class CachingMetadataReaderFactoryPostProcessor
implements BeanDefinitionRegistryPostProcessor, PriorityOrdered {
@Override
public int getOrder() {
// Must happen before the ConfigurationClassPostProcessor is created
return Ordered.HIGHEST_PRECEDENCE;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
register(registry);
configureConfigurationClassPostProcessor(registry);
}
private void register(BeanDefinitionRegistry registry) {
// 根据bean的类型和对应构造方法创建一个bean定义
BeanDefinition definition = BeanDefinitionBuilder
.genericBeanDefinition(SharedMetadataReaderFactoryBean.class, SharedMetadataReaderFactoryBean::new)
.getBeanDefinition();
// 进行bean定义的注册
registry.registerBeanDefinition(BEAN_NAME, definition);
}
private void configureConfigurationClassPostProcessor(BeanDefinitionRegistry registry) {
try {
// 尝试获取bean
BeanDefinition definition = registry
.getBeanDefinition(AnnotationConfigUtils.CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME); // 给bean添加属性
definition.getPropertyValues().add("metadataReaderFactory", new RuntimeBeanReference(BEAN_NAME));
}
catch (NoSuchBeanDefinitionException ex) {
}
}
}
/**
* {@link FactoryBean} to create the shared {@link MetadataReaderFactory}.
*/
static class SharedMetadataReaderFactoryBean
implements FactoryBean<ConcurrentReferenceCachingMetadataReaderFactory>, BeanClassLoaderAware,
ApplicationListener<ContextRefreshedEvent> {
private ConcurrentReferenceCachingMetadataReaderFactory metadataReaderFactory;
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.metadataReaderFactory = new ConcurrentReferenceCachingMetadataReaderFactory(classLoader);
}
@Override
public ConcurrentReferenceCachingMetadataReaderFactory getObject() throws Exception {
return this.metadataReaderFactory;
}
@Override
public Class<?> getObjectType() {
return CachingMetadataReaderFactory.class;
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
this.metadataReaderFactory.clearCache();
}
}
3. ContextIdApplicationContextInitializer设置容器ID
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ContextId contextId = getContextId(applicationContext);
// 设置容器ID
applicationContext.setId(contextId.getId());
// 并注册一个单例bean
applicationContext.getBeanFactory().registerSingleton(ContextId.class.getName(), contextId);
}
4. ConfigurationWarningsApplicationContextInitializer添加bean工厂后置处理器
用于报告常见的错误配置
@Override
public void initialize(ConfigurableApplicationContext context) {
context.addBeanFactoryPostProcessor(new ConfigurationWarningsPostProcessor(getChecks()));
}
/**
* Returns the checks that should be applied.
* @return the checks to apply
*/
protected Check[] getChecks() {
return new Check[] { new ComponentScanPackageCheck() };
}
5. ServerPortInfoApplicationContextInitializer添加自己为监听器
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addApplicationListener(this);
}
监听WebServerInitializedEvent事件,然后设置属性,比如local.server.port
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
String propertyName = "local." + getName(event.getApplicationContext()) + ".port";
setPortProperty(event.getApplicationContext(), propertyName, event.getWebServer().getPort());
}
- ConditionEvaluationReportLoggingListener添加监听器并设置autoConfigurationReport
@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());
}
}
发布ApplicationContextInitializedEvent事件
@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
Executor executor = getTaskExecutor();
// 根据事件类型和
for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}
此处两个Listener在此处没啥用处,后置对ApplicationEnvironmentPreparedEvent感兴趣。
答应启动信息
获取一些环境信息打印到控制台
2020-05-30 22:38:13.208 INFO 14424 --- [ main] c.e.m.ManagingTransactionsApplication : Starting ManagingTransactionsApplication on darren-PC with PID 14424 (D:\springboot\gs-managing-transactions-master\complete\target\classes started by 周光来 in D:\springboot\gs-managing-transactions-master\complete)
2020-05-30 22:39:50.580 INFO 14424 --- [ main] c.e.m.ManagingTransactionsApplication : No active profile set, falling back to default profiles: default
加载所有资源
这里的资源指的是通过SpringApplication的构造或者通过直接调用setSources传入的类
前者为primarySources,后者为sources,当前仅仅为传入的主类
/**
* Return an immutable set of all the sources that will be added to an
* ApplicationContext when {@link #run(String...)} is called. This method combines any
* primary sources specified in the constructor with any additional ones that have
* been {@link #setSources(Set) explicitly set}.
* @return an immutable set of all sources
*/
public Set<Object> getAllSources() {
Set<Object> allSources = new LinkedHashSet<>();
if (!CollectionUtils.isEmpty(this.primarySources)) {
allSources.addAll(this.primarySources);
}
if (!CollectionUtils.isEmpty(this.sources)) {
allSources.addAll(this.sources);
}
return Collections.unmodifiableSet(allSources);
}
加载bean到上下文中
/**
* Load beans into the application context.
* @param context the context to load beans into
* @param sources the sources to load
*/
protected void load(ApplicationContext context, Object[] sources) {
if (logger.isDebugEnabled()) {
logger.debug("Loading source " + StringUtils.arrayToCommaDelimitedString(sources));
}
BeanDefinitionLoader loader = createBeanDefinitionLoader(getBeanDefinitionRegistry(context), sources);
if (this.beanNameGenerator != null) {
loader.setBeanNameGenerator(this.beanNameGenerator);
}
if (this.resourceLoader != null) {
loader.setResourceLoader(this.resourceLoader);
}
if (this.environment != null) {
loader.setEnvironment(this.environment);
}
// 指定定义的加载
loader.load();
}
/**
* Factory method used to create the {@link BeanDefinitionLoader}.
* @param registry the bean definition registry
* @param sources the sources to load
* @return the {@link BeanDefinitionLoader} that will be used to load beans
*/
protected BeanDefinitionLoader createBeanDefinitionLoader(BeanDefinitionRegistry registry, Object[] sources) {
return new BeanDefinitionLoader(registry, sources);
}
通过创建BeanDefinitionLoader来加载bean
/**
* Create a new {@link BeanDefinitionLoader} that will load beans into the specified
* {@link BeanDefinitionRegistry}.
* @param registry the bean definition registry that will contain the loaded beans
* @param sources the bean sources
*/
BeanDefinitionLoader(BeanDefinitionRegistry registry, Object... sources) {
Assert.notNull(registry, "Registry must not be null");
Assert.notEmpty(sources, "Sources must not be empty");
this.sources = sources;
// 在构造此类时会尝试注册一些与注解相关的后置处理器 但是在容器构造时重复
this.annotatedReader = new AnnotatedBeanDefinitionReader(registry);
this.xmlReader = new XmlBeanDefinitionReader(registry);
if (isGroovyPresent()) {
this.groovyReader = new GroovyBeanDefinitionReader(registry);
}
this.scanner = new ClassPathBeanDefinitionScanner(registry);
// 添加类排除过滤器 也就是后面scanner不会再扫描这些主类了
this.scanner.addExcludeFilter(new ClassExcludeFilter(sources));
}
如果是Class类,需要看是否包含注解,当然不一定需要有注解
private int load(Class<?> source) {
if (isGroovyPresent() && GroovyBeanDefinitionSource.class.isAssignableFrom(source)) {
// Any GroovyLoaders added in beans{} DSL can contribute beans here
GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class);
load(loader);
}
// 判断是否包含注解Component 但并不一定需要这个注解
if (isComponent(source)) {
this.annotatedReader.register(source);
return 1;
}
return 0;
}
private boolean isComponent(Class<?> type) {
// This has to be a bit of a guess. The only way to be sure that this type is
// eligible is to make a bean definition out of it and try to instantiate it.
if (AnnotationUtils.findAnnotation(type, Component.class) != null) {
return true;
}
// Nested anonymous classes are not eligible for registration, nor are groovy
// closures
if (type.getName().matches(".*\\$_.*closure.*") || type.isAnonymousClass() || type.getConstructors() == null
|| type.getConstructors().length == 0) {
return false;
}
return true;
}
如果isComponent判断返回true,就会进行BeanDefinition的注册,参看源码
org.springframework.context.annotation.AnnotatedBeanDefinitionReader#register,此处不展开了
发布ApplicationPreparedEvent事件
总结
在Spring Boot创建了容器对象之后,在这里主要进行容器的一些初始化操作,比如设置环境属性、注册一些辅助的单例对象、注册转换服务类等,当然最重要的还是加载主资源类并注册到BeanFactory中(Spring Boot项目就是SpringApplication构造的时候传入的,而Spring Cloud就是BootstrapImportSelectorConfiguration类,这些在Spring容器的fresh阶段会做为ConfigurationClassPostProcessor
处理的入口类),这里有两个扩展的地方,一个是在加载主类之前,一个是在加载主类之后,监听这两个事件都可以获取到容器对象。有一个重要的扩展点就是可以通过这些监听然后注册BeanFactoryPostProcessor
到容器中。