这是我参与更文挑战的第5天,活动详情查看: 更文挑战spring
IoC容器是Spring的核心模块,是抽象了对象管理、依赖关系管理的框架解决方案。Spring 提供了不少 的容器,其中 BeanFactory 是顶层容器(根容器),不能被实例化,它定义了全部 IoC 容器 必须听从 的一套原则,具体的容器实现能够增长额外的功能,好比咱们经常使用到的ApplicationContext,其下更具 体的实现如 ClassPathXmlApplicationContext 包含了解析 xml 等一系列的内容,
AnnotationConfigApplicationContext 则是包含了注解解析等一系列的内容。Spring IoC 容器继承体系 很是聪明,须要使用哪一个层次用哪一个层次便可,没必要使用功能大而全的。
BeanFactory 顶级接口方法栈以下markdown
BeanFactory 容器继承体系app
经过其接口设计,咱们能够看到咱们一向使用的 ApplicationContext 除了继承BeanFactory的子接口, 还继承了ResourceLoader、MessageSource等接口,所以其提供的功能也就更丰富了。
下面咱们以 ClasspathXmlApplicationContext 为例,深刻源码说明 IoC 容器的初始化流程。框架
思路:建立一个类 LagouBean ,让其实现几个特殊的接口,并分别在接口实现的构造器、接口方法中 断点,观察线程调用栈,分析出 Bean 对象建立和管理关键点的触发时机。
TestBean类:ide
package com.lagou;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
/** * @Author yuyuyu * @create 2021/06/07 11:46 */
public class TestBean implements InitializingBean{
/** * 构造函数 */
public LagouBean(){
System.out.println("TestBean 构造器...");
}
/** * InitializingBean 接口实现 */
public void afterPropertiesSet() throws Exception {
System.out.println("TestBean afterPropertiesSet...");
}
}
复制代码
BeanPostProcessor 接口实现类函数
package com.lagou;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
/** * @Author yuyuyu * @create 2019/06/07 16:59 */
public class MyBeanPostProcessor implements BeanPostProcessor {
public MyBeanPostProcessor() {
System.out.println("BeanPostProcessor 实现类构造函数...");
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if("LagouBean".equals(beanName)) {
System.out.println("BeanPostProcessor 实现类 postProcessBeforeInitialization 方法被调用中......");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if("LagouBean".equals(beanName)) {
System.out.println("BeanPostProcessor 实现类 postProcessAfterInitialization 方法被调用中......"); }
return bean;
}
}
复制代码
BeanFactoryPostProcessor 接口实现类源码分析
package com.lagou;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
/** * @Author yuyuyu * @create 2021/06/07 16:56 */
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public MyBeanFactoryPostProcessor() {
System.out.println("BeanFactoryPostProcessor的实现类构造函数...");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryPostProcessor的实现方法调用中......"); }
}
复制代码
applicationContext.xmlpost
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="lagouBean" class="com.lagou.LagouBean"/> <bean id="myBeanFactoryPostProcessor" class="com.lagou.MyBeanFactoryPostProcessor"/> <bean id="myBeanPostProcessor" class="com.lagou.MyBeanPostProcessor"/> </beans>
复制代码
IoC 容器源码分析用例ui
/** * Ioc 容器源码分析基础案例 */
@Test
public void testIoC() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
LagouBean lagouBean = applicationContext.getBean(LagouBean.class);
System.out.println(lagouBean);
}
复制代码
(1)分析 Bean 的建立是在容器初始化时仍是在 getBean 时this
根据断点调试,咱们发现,在未设置延迟加载的前提下,Bean 的建立是在容器初始化过程当中完成的。
(2)分析构造函数调用状况
经过如上观察,咱们发现构造函数的调用时机在AbstractApplicationContext类refresh方法的 finishBeanFactoryInitialization(beanFactory)处;
(3)分析 InitializingBean 之 afterPropertiesSet 初始化方法调用状况
经过如上观察,咱们发现 InitializingBean中afterPropertiesSet 方法的调用时机也是在 AbstractApplicationContext类refresh方法的finishBeanFactoryInitialization(beanFactory);
(4)分析BeanFactoryPostProcessor 初始化和调用状况 分别在构造函数、postProcessBeanFactory 方法处打断点,观察调用栈,
发现 BeanFactoryPostProcessor 初始化在AbstractApplicationContext类refresh方法的 invokeBeanFactoryPostProcessors(beanFactory);
postProcessBeanFactory 调用在AbstractApplicationContext类refresh方法的 invokeBeanFactoryPostProcessors(beanFactory);
(5)分析 BeanPostProcessor 初始化和调用状况
分别在构造函数、postProcessBeanFactory 方法处打断点,观察调用栈,发现
BeanPostProcessor 初始化在AbstractApplicationContext类refresh方法的 registerBeanPostProcessors(beanFactory);
postProcessBeforeInitialization 调用在AbstractApplicationContext类refresh方法的 finishBeanFactoryInitialization(beanFactory);
postProcessAfterInitialization 调用在AbstractApplicationContext类refresh方法的 finishBeanFactoryInitialization(beanFactory);
(6)总结
根据上面的调试分析,咱们发现 Bean对象建立的几个关键时机点代码层级的调用都在 AbstractApplicationContext 类 的 refresh 方法中,可⻅这个方法对于Spring IoC 容器初始化来讲至关 关键,汇总以下:
关键点 | 触发代码 |
---|---|
构造器 | refresh#finishBeanFactoryInitialization(beanFactory)(beanFactory) |
BeanFactoryPostProcessor 初始化 | refresh#invokeBeanFactoryPostProcessors(beanFactory) |
BeanFactoryPostProcessor 方法调用 | refresh#invokeBeanFactoryPostProcessors(beanFactory) |
BeanPostProcessor 初始化 | registerBeanPostProcessors(beanFactory) |
BeanPostProcessor 方法调用 | refresh#finishBeanFactoryInitialization(beanFactory) |
由上分析可知,Spring IoC 容器初始化的关键环节就在 AbstractApplicationContext#refresh() 方法中 ,咱们查看 refresh 方法来俯瞰容器建立的主体流程,主体流程下的具体子流程咱们后面再来讨论。
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 第一步:刷新前的预处理
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
/* 第二步: 获取BeanFactory;默认实现是DefaultListableBeanFactory 加载BeanDefition 并注册到 BeanDefitionRegistry */
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
// 第三步:BeanFactory的预准备工做(BeanFactory进行一些设置,好比context的类加 载器等)
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
// 第四步:BeanFactory准备工做完成后进行的后置处理工做
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
// 第五步:实例化并调用实现了BeanFactoryPostProcessor接口的Bean
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
// 第六步:注册BeanPostProcessor(Bean的后置处理器),在建立bean的先后等执行
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
// 第七步:初始化MessageSource组件(作国际化功能;消息绑定,消息解析)
initMessageSource();
// Initialize event multicaster for this context.
// 第八步:初始化事件派发器
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
// 第九步:子类重写这个方法,在容器刷新的时候能够自定义逻辑
onRefresh();
// Check for listener beans and register them.
// 第十步:注册应用的监听器。就是注册实现了ApplicationListener接口的监听器
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
/* 第十一步: 初始化全部剩下的非懒加载的单例bean 初始化建立非懒加载方式的单例Bean实例(未设置属性) 填充属性 初始化方法调用(好比调用afterPropertiesSet方法、init-method方法) 调用BeanPostProcessor(后置处理器)对实例bean进行后置处 */
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
}
}
复制代码