Spring Boot框架核心方法refresh
Posted work hard work smart
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot框架核心方法refresh相关的知识,希望对你有一定的参考价值。
refresh方法介绍
bean配置读取和加载入口
在这个方法内完成sping框架启动流程
首先从SpringBoot的启动run方法,进入到AbstractApplicationContext类refresh方式涉及到下面多个子方法的调用
@Override public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. prepareRefresh(); // Tell the subclass to refresh the internal bean factory. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation. registerBeanPostProcessors(beanFactory); // Initialize message source for this context. initMessageSource(); // Initialize event multicaster for this context. initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses. onRefresh(); // Check for listener beans and register them. registerListeners(); // Instantiate all remaining (non-lazy-init) singletons. finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event. finishRefresh(); } catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn("Exception encountered during context initialization - " + "cancelling refresh attempt: " + ex); } // Destroy already created singletons to avoid dangling resources. destroyBeans(); // Reset \'active\' flag. cancelRefresh(ex); // Propagate exception to caller. throw ex; } finally { // Reset common introspection caches in Spring\'s core, since we // might not ever need metadata for singleton beans anymore... resetCommonCaches(); } } }
1、prepareRefresh();
容器状态设置
初始化属性设置
检查必备属性是否存在
2、obtainFreshBeanFactory
设置beanFactory序列化id
获取beanFactory
3、prepareBeanFactory(beanFactory);
设置beanFactory一些属性
添加后置处理器
设置忽略的自动装配接口
注册一些主件
4、postProcessBeanFactory(beanFactory);
子类重写以在BeanFactory完成创建后做进一步设置
5、invokeBeanFactoryPostProcessors(beanFactory);
调用BeanDefinitionRegistryPostProcessor实现向容器内添加bean的定义 (使用参考: 实现BeanDefinitionRegistryPostProcessor)
调用BeanFactoryPostProcessor实现向容器内添加bean的定义添加属性
BeanFactoryPostProcessor的使用
1)创建Teacher类
@Component public class Teacher { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
2)创建类MyBeanFactoryPostProcessor ,实现BeanFactoryPostProcessor接口。并且给teacher增加name属性值为Nick
@Component public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor{ @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { BeanDefinition teacher = configurableListableBeanFactory.getBeanDefinition("teacher"); MutablePropertyValues propertyValues = teacher.getPropertyValues(); propertyValues.addPropertyValue("name","Nick"); } }
3)运行测试方法testTeacher
结果为
6、registerBeanPostProcessors(beanFactory);
找到BeanPostPocessor的实现
排序后注册进容器内
7、initMessageSource();
初始化国际化相关属性
8、 initApplicationEventMulticaster();
初始化事件广播器。注册到容器当中
9、onRefresh();
创建web容器。如果是web环境当中,会进入如下图的方法,构建一个web容器,如tomcat,jetty等。
10、registerListeners();
添加容器内事件监听器至事件广播器中
派发早期事件
11、finishBeanFactoryInitialization(beanFactory);
初始化剩下的单实例Bean
12、finishRefresh();
初始化生命周期处理器
调用生命周期处理器的onRefresh方法
发布ContextRefreshedEvent 事件
JMX相关处理
13、 resetCommonCaches();
清除缓存
以上是关于Spring Boot框架核心方法refresh的主要内容,如果未能解决你的问题,请参考以下文章
spring boot框架学习4-spring boot核心
spring boot框架学习3-spring boot核心