Spring源码系列文章
Spring源码解析(一):环境搭建
Spring源码解析(二):bean容器的创建、默认后置处理器、扫描包路径bean
Spring源码解析(三):bean容器刷新
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// 1、刷新前预处理
prepareRefresh();
// 2、获取最新的bean工厂
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// 3、注册bean工厂的类加载器和部分后置处理器
prepareBeanFactory(beanFactory);
try {
// 4、bean工厂的后置处理器(子类实现)
postProcessBeanFactory(beanFactory);
// 5、执行bean工厂的后置处理器
invokeBeanFactoryPostProcessors(beanFactory);
// 6、注册所有的后置处理器
registerBeanPostProcessors(beanFactory);
// 7、初始化国际化组件
initMessageSource();
// 8、初始化事件多播器(用来广播事件)
initApplicationEventMulticaster();
// 9、子类实现,扩展其他bean
onRefresh();
// 10、注册监听器
registerListeners();
// 11、实例化,初始化所有的非懒加载的单例bean
finishBeanFactoryInitialization(beanFactory);
// 12、最后刷新,发布相应事件
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// 销毁已创建的单例bean
destroyBeans();
// 重置容器激活标签
cancelRefresh(ex);
// 抛异常
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
protected void prepareRefresh() {
//设置启动时间
this.startupDate = System.currentTimeMillis();
//容器是否关闭
this.closed.set(false);
//容器是否激活
this.active.set(true);
//根据日志开启级别打印日志
if (logger.isDebugEnabled()) {
if (logger.isTraceEnabled()) {
logger.trace("Refreshing " + this);
}
else {
logger.debug("Refreshing " + getDisplayName());
}
}
// 1.空实现,子类实现。AnnotationConfigApplicationContext容器的子容器初始化工作。
initPropertySources();
// 2.创建环境对象,验证需要的属性文件是否都放到环境中
getEnvironment().validateRequiredProperties();
// 3.准备监听器集合,如果为空则创建,如果存在则清空再创建
if (this.earlyApplicationListeners == null) {
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
}
else {
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}
// 4.创建刷新前的事件集合
// 将在Multicast可用后发布
this.earlyApplicationEvents = new LinkedHashSet<>();
}
初始化前
从它setxxx方法获取对应上下文东西protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// 1.设置类加载器:存在则直接设置/不存在则新建一个默认类加载器
beanFactory.setBeanClassLoader(getClassLoader());
// 2.设置EL表达式解析器(Bean初始化完成后填充属性时会用到)
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
// 3.设置属性注册解析器PropertyEditor
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
// 4.将当前的ApplicationContext对象交给ApplicationContextAwareProcessor类来处理,
// 从而在Aware接口实现类中的注入context
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
//设置忽略自动装配的接口
//因为在ApplicationContextAwareProcessor中已经完成了手动注入
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
/**
* 注册几个自动装配相关的类和实例
* 5.在应用代码就可以通过类型自动装配把工厂实例和ApplicationContext实例设置到自定义bean的属性中
*
* 例如:这几个属性都会被自动设置,虽然没有在显示的在bean定义xml中注入它们
* @Autowired
* private BeanFactory beanFactory;
* @Autowired
* private ApplicationContext appContext;
*/
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
// 6.注册监听后置处理器
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
// 7.添加编译时AspectJ的支持
// 当容器检查到定义了名称为loadTimeWeaver的bean时
// 会注册一个LoadTimeWeaverAwareProcessor到容器中
// 这个后置处理器用来处理LoadTimeWeaverAware接口的
if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
// 将以下三个bean放入singletonObjects单例bean容器中
// 8.注册当前容器环境environment组件Bean
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
// 9.注册系统配置systemProperties组件Bean
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
// 10.注册系统环境systemEnvironment组件Bean
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}
}
bean实例化之前
注册后置处理器(类似prepareBeanFactory方法中的beanFactory.addBeanPostProcessor)用于修改bean的定义信息
getBeanFactoryPostProcessors():返回bean工厂后置处理器列表,这里是个空集合
下面主要说下代码中添加bean工厂后置处理器组件的方式
修改
应用程序上下文的内部 Bean 工厂
未实例化
任何 bean覆盖或添加属性
故此子类优先执行
)修改
应用程序上下文的内部 Bean 定义注册信息
未实例化
任何 bean添加更多的 Bean 定义
实例化前
,实例化后就没有意义了invokeBeanFactoryPostProcessors():执行所有bean定义和工厂后置处理器
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
// 已经执行过的bean工厂或定义后置处理器名称集合
Set<String> processedBeans = new HashSet<>();
// 此时beanFactory是DefaultListableBeanFactory,实现了BeanDefinitionRegistry,所以返回true
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
// 用于存放bean工厂后置处理器集合
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
// 用于存放bean定义后置处理器集合
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
// 1.这里处理的是 入参的bean工厂自定义后置处理器,需要添加到容器属性集合中,不怎么用,这里不说了
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else {
regularPostProcessors.add(postProcessor);
}
}
// 用于保存本次要执行的bean定义后置处理器
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
// 2.首先,先执行实现了PriorityOrdered的bean定义后置处理器
// 2.1.找出所有实现BeanDefinitionRegistryPostProcessor接口的Bean的beanName
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
// 2.2.校验是否实现了PriorityOrdered接口
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
// 2.3.获取ppName对应的bean实例, 添加到currentRegistryProcessors中
// beanFactory.getBean();获取bean,获取不到创建。后续单独将
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
// 2.4.将要被执行的加入processedBeans,避免后续重复执行
processedBeans.add(ppName);
}
}
// 2.5.进行排序(之前注册默认处理器时候,添加过排序策略属性 )
// beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
sortPostProcessors(currentRegistryProcessors, beanFactory);
// 2.6.添加到bean定义后置处理器
registryProcessors.addAll(currentRegistryProcessors);
// 2.7.将所有bean定义后置处理器遍历执行它的方法 postProcessBeanDefinitionRegistry()
// 对beanBefinition的增删改
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
// 2.8.执行完毕后, 清空currentRegistryProcessors
currentRegistryProcessors.clear();
// 3.接下来,先执行实现了Ordered的bean定义后置处理器
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
// 3.1.校验是否实现了Ordered接口,并且还未执行过
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
// 4.最后,执行剩下的bean定义后置处理器,什么排序接口都没有实现
boolean reiterate = true;
while (reiterate) {
reiterate = false;
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
// 4.1.跳过已经执行过的
if (!processedBeans.contains(ppName)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
// 4.2.如果有bean定义后置处理器被执行, 则有可能会产生新的bean定义后置处理器
// 因此这边将reiterate赋值为true, 代表需要再循环查找一次
reiterate = true;
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
}
// 5.现在统一执行bean定义后置处理器的postProcessBeanFactory方法
//(BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcessor)
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
// 6.现在统一执行入参bean定义后置处理器的postProcessBeanFactory方法
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
}
else {
// 如果没有bean定义后置处理器,直接执行参数传入的bean定义后置处理器
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}
// 7.到这里 , 入参beanFactoryPostProcessors和容器中的所有BeanDefinitionRegistryPostProcessor已经全部处理完毕
// 下面开始处理容器中的所有BeanFactoryPostProcessor
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
...与上面代码一模一样,下面总集说
}
总结
先介绍下所有bean后置处理器接口
主要后置处理器执行位置:
实例化前后
执行初始化前后
执行处理流程方面和bean工厂后置处理器大致一样
// 将所有的BeanPostProcessor注册到BeanFactory中
public static void registerBeanPostProcessors
(ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
// 1.获取beanFactory中所有的BeanPostProcessor类型的bean的名字
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
// 2.注册一个 BeanPostProcessorChecker,用来记录 bean 在 BeanPostProcessor 实例化时的信息
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
// 3.对所有的BeanPostProcessor根据实现不同的接口(PriorityOrdered,Ordered等)进行分离
// 3.1.用于存放实现了PriorityOrdered的后处理器集合
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
// 3.2.用于存放实现了PriorityOrdered并且是MergedBeanDefinitionPostProcessor类型的后处理器集合
List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
// 3.3.用于存放实现了Ordered的后处理器集合
List<String> orderedPostProcessorNames = new ArrayList<>();
// 3.4.用于存放没有实现排序接口的后处理器集合
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
// 先添加到Priority接口集合中
priorityOrderedPostProcessors.add(pp);
// 如果是合并定义处理器再添加到对应集合中
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
// 4.首先,排序实现了ProorityOrdered接口的BeanPostProcessor
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
// 注册(将BeanPostProcessor加入到BeanFactory中beanFactory.addBeanPostProcessor(postProcessor))
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// 5.其次注册实现了Ordered接口的BeanPostProcessor
List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String ppName : orderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
orderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
sortPostProcessors(orderedPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
// 6.然后,注册剩余的所有普通的BeanPostProcessor
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
for (String ppName : nonOrderedPostProcessorNames) {
BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
// 7.最后,注册所有内部的BeanPostProcessor
sortPostProcessors(internalPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
//先删除再注册ApplicationListenerDetector(主要是为了移动到处理器链的末尾)
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
总结
protected void registerListeners() {
// 添加实现ApplicationListener作为侦听器的bean。
// 不会影响其他侦听器,可以将它们添加为非bean。
// 先注册静态指定的监听器
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// 在事件多播器上添加监听 并没有执行
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// 发布早期的时间,并且将 earlyApplicationEvents 设置为空
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// 1.初始化此上下文的转换服务
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
// 2.如果beanFactory之前没有注册嵌入值解析器,则注册默认的嵌入值解析器:主要用于注解属性值的解析。
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// 3.尽早初始化LoadTimeWeaverAware bean,以便尽早注册它们的转换器
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
// 4.禁止使用临时类加载器进行类型匹配
beanFactory.setTempClassLoader(null);
// 5.允许缓存所有的bean的定义数据
beanFactory.freezeConfiguration();
// 6.实例化所有剩余(非懒加载)单例对象
beanFactory.preInstantiateSingletons();
}
具体实例化过程
拆分到后面章节单独讲
protected void finishRefresh() {
// 清理缓存信息
clearResourceCaches();
// 初始化生命周期处理器
initLifecycleProcessor();
// 将刷新事件传播到生命周期处理器
getLifecycleProcessor().onRefresh();
// 发布容器刷新完毕事件到相应的监听器
publishEvent(new ContextRefreshedEvent(this));
// 略
LiveBeansView.registerApplicationContext(this);
}
初始化生命周期处理器
使用监听器
1、注册事件
public class MyApplicationEvent extends ApplicationEvent {
private static final long serialVersionUID = 5366526231219883438L;
private String message;
public MyApplicationEvent(Object source, String message) {
super(source);
this.message = message;
}
public String getMessage() {
return message;
}
}
2、注册监听器
@Component
public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {
@Override
public void onApplicationEvent(MyApplicationEvent event) {
System.out.println("MyApplicationListener 收到消息: " + event.getMessage());
}
}
3、发布事件
@Component
public class MyAnnotationApplicationListener {
@EventListener(classes = MyApplicationEvent.class)
public void myApplicationEventListener(MyApplicationEvent event) {
System.out.println("使用注解的方式, 收到事件: " + event.getMessage());
}
}
4、使用
因为 AnnotationConfigApplicationContext 实现了 ApplicationContext , 而 ApplicationContext 实现了 ApplicationEventPublisher,所以这块可以传入当前 context。
public class Client {
public static void main(String[] args) {
//创建注解容器,入参为配置类
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
MyApplicationEvent event = new MyApplicationEvent(context,"呼叫。。。");
context.publishEvent(event);
//关闭容器
context.close();
}
}
destroyBeans();销毁单例bean
protected void clearSingletonCache() {
synchronized (this.singletonObjects) {
this.singletonObjects.clear();// 单例对象缓存的map
this.singletonFactories.clear();
this.earlySingletonObjects.clear();
this.registeredSingletons.clear();
this.singletonsCurrentlyInDestruction = false;
}
}
cancelRefresh(ex);设置容器激活状态为否