在内部调用了WebApplicationContextUtils.initServletPropertySources方法,由名称可得知,它用于初始化Servlet的属性资源,在实际执行过程中分别根据ServletContext和ServletConfig的值来判定是否要将指定的配置包装为ServletContextPropertySource。在实际调试过程中他们的值都为空,也就是没有进行任何操作。
② - 检查必备属性,此处是用于检查哪些属性是必不可少的,例如可以设置"example.address"这个属性必须不为空。
③ - 重新对监听器排序
prepareBeanFactory(beanFactory)
// AbstractApplicationContext.java
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// 设置类加载器
// Tell the internal bean factory to use the context's class loader etc.
beanFactory.setBeanClassLoader(getClassLoader());
// 设置SpringEL表达式解析器
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
// 设置属性编辑器注册
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
// ① 忽略指定的接口注入
// Configure the bean factory with context callbacks.
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
// 指定对特定接口注入时实际的注入对象,例如有某对象想要注入BeanFactory,则实际将会指定它注入的是当前设置的BeanFactory
// BeanFactory interface not registered as resolvable type in a plain factory.
// MessageSource registered (and found for autowiring) as a bean.
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
// 添加和移除ApplicationListener
// Register early post-processor for detecting inner beans as ApplicationListeners.
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
//
// Detect a LoadTimeWeaver and prepare for weaving, if found.
if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
// Set a temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
// 注册默认环境对象
// Register default environment beans.
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
// 注册系统配置对象
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
// 注册系统环境对象
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}
}
① - 这里的忽略依赖接口,是指这些Aware接口的实现类在Spring中将会自动忽略接口实现类中和setter方法入参相同的类型,举例说明
public interface EnvironmentAware extends Aware {
void setEnvironment(Environment environment);
}
public class MyEnvironmentAware implements Environment {
private Environment environment;
@Overwired
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}
示例中展示了如何使用EnvironmentAware接口来实现在自定义代码中获取Environment,上面所说的忽略,是指在Spring自动装配MyEnvironment这个类的时候,会自动忽略到setEnvironment方法中的Environment对象注入。
在忽略接口的第一行代码添加了一个ApplicationContextAwareProcessor,而它则是Spring框架统一来设置这些Aware接口实现类的处理器。
postProcessBeanFactory(beanFactory)
在当前AbstractApplicationContext类中的postProcessBeanFactory方法并未实现,由其子类实现。
// AnnotationConfigServletWebServerApplicationContext.java
// 实现一
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// ①
super.postProcessBeanFactory(beanFactory);
// ②
if (this.basePackages != null && this.basePackages.length > 0) {
this.scanner.scan(this.basePackages);
}
// ③
if (!this.annotatedClasses.isEmpty()) {
this.reader.register(ClassUtils.toClassArray(this.annotatedClasses));
}
}