码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot框架核心方法refresh

时间:2020-02-26 20:44:09      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:title   环境   return   rtu   dev   清除   设置   htm   ecif   

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的定义

 调用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

 Spring Boot bean实例化流程

 

12、finishRefresh();

初始化生命周期处理器

调用生命周期处理器的onRefresh方法

发布ContextRefreshedEvent 事件

JMX相关处理

 

13、 resetCommonCaches();

清除缓存

Spring Boot框架核心方法refresh

标签:title   环境   return   rtu   dev   清除   设置   htm   ecif   

原文地址:https://www.cnblogs.com/linlf03/p/12366284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!