前言
接下来我们编写入口代码,跟代码梳理一下Spring IOC一些重要的方法节点。IOC有12个比较重要的方法,把这12个方法学习一下,整个Spring IOC基本就差不多了。
编写Spring IOC入口
1、创建需要被管理的类
接口类:
package service;public interface Person {public void work();public void sleep();
}
实现类:
package service.impl;
import service.Person;public class Teacher implements Person {private String name;private String age;@Overridepublic void work() {System.out.printf("我是:"+name+",今年:"+age+":工作中");}@Overridepublic void sleep() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}
}
2、编写配置文件
resources下创建application-context.xml配置文件:
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean name="teacher" class="service.impl.Teacher"><property name="name" value="张三"></property><property name="age" value="30"></property></bean></beans>
3、入口
package controller.main;import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.impl.Teacher;public class PersonTest {public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("application-context.xml");Teacher teacher = (Teacher) applicationContext.getBean("teacher");teacher.work();}
}
4、debug
我们主要看refresh()方法中的核心子方法;
@Overridepublic 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();}}}
流程节点简介
prepareRefresh();
这个方法就是IOC正式处理前做一些准备工作,设置容器当前的状态、时间、实例化一些数组缓存。
obtainFreshBeanFactory();
该方法在里面算是比较核心的一个,就是解析配置、准备好我们要实例化、初始化的材料。像我们这个按时使用的是xml配置的方法,所以在里面会对xml文件做一个解析工作,解析每个Bean别名、类路径、属性值等等,并将其封装成BeanDefinition对象,存入缓存中,为后续实例化、初始化环节做准备。
prepareBeanFactory(beanFactory);
该方法用于设置添加或忽略一些postProcessor,为后续执行相关的PostProcessor作准备,因为在遍历执行PostProcessor时,有些方法是不需要执行,或者需要执行。
postProcessBeanFactory(beanFactory);
这个方法没有具体的实现,是一个拓展接口。使用者可以自己实现ApplicationContext接口,对其进行一些拓展性操作;
invokeBeanFactoryPostProcessors(beanFactory);
这个方法会遍历所有的实现BeanFactoryPostProcessors的类,并调用其postProcessBeanFactory方法,开发者可以通过实现BeanFactoryPostProcessors接口,对当前上下文进行操作,比如添加实例化初始化需要的对象BeanDefinition等;在这个过程中首先会查BeanFactoryPostProcessors相关容器中是否有这个对象,并根据是否实现Order等接口进行排序,排序完成后会提前对BeanFactoryPostProcessors相关类进行实例化初始化操作,最后调用处理方法。还有看我们的XML配置文件中的类是否也有实现BeanFactoryPostProcessors接口的,有也按排序、实例化初始化、调用等顺序进行。而普通的Bean都是在后续的步骤中进行实例化的。
registerBeanPostProcessors(beanFactory);
该方法用于设置添加postProcessor,为后续执行相关的PostProcessor作准备,这个步骤跟prepareBeanFactory(beanFactory)方法差不多。
initMessageSource();
这个方法主要作用就是使用国际化,定制不同的消息文本,比如定义了一个Person的Bean,它有name属性,我们需要在不同的国家展示对应国家所在语言名称,这时候就可以使用国际化了。
initApplicationEventMulticaster();
初始化应用事件广播器。这是观察者模式得典型应用。我们知道观察者模式由主题Subject和Observer组成。广播器相当于主题Subject,其包含多个监听器。当主题发生变化时会通知所有的监听器。
onRefresh();
onRefresh 是一个为使用者进行扩展的方法,springboot就对该方法进行了处理。
registerListeners();
// Register statically specified listeners first.for (ApplicationListener<?> listener : getApplicationListeners()) {getApplicationEventMulticaster().addApplicationListener(listener);}// Do not initialize FactoryBeans here: We need to leave all regular beans// uninitialized to let post-processors apply to them!String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);for (String listenerBeanName : listenerBeanNames) {getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);}// Publish early application events now that we finally have a multicaster...Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;this.earlyApplicationEvents = null;if (earlyEventsToProcess != null) {for (ApplicationEvent earlyEvent : earlyEventsToProcess) {getApplicationEventMulticaster().multicastEvent(earlyEvent);}}
这个方法逻辑比较简单,就是将内部的、以及我们自定义的监听器添加到缓存中,为后续逻辑处理做准备。还有一个就是添加事件源到缓存中。
finishBeanFactoryInitialization(beanFactory);
这个方法就是IOC的核心,实例化剩下非懒加载的Bean。我们前面的方法中例如PostProcessor方法会提前实例化好,所以在这块并不会进行实例化。方法会去遍历BeanDefinition缓存集合,判断是否是FactoryBean的子类,是则调用其getObjects()方法直接生成对象,不走createBean步骤,非FactoryBean子类的则走常规的createBean创建过程。实例化Bean及其父类、属性值,实例化步骤完成后进入填充属性环节,会对依赖进行注入。BeanPostProcessor方法也会在这个环节执行调用。
1、遍历所有被加载到缓存中的 beanName,触发所有剩余的非懒加载单例 bean 的实例化。
2、首先通过 beanName 尝试从缓存中获取,如果存在则跳过实例化过程;否则,进行 bean 的实例化。
3、根据 BeanDefinition,使用构造函数创建 bean 实例。
4、根据 BeanDefinition,进行 bean 实例属性填充。
5、执行 bean 实例的初始化。
5.1、触发 Aware 方法。
5.2、触发 BeanPostProcessor 的 postProcessBeforeInitialization 方法。
5.3、如果 bean 实现了 InitializingBean 接口,则触发 afterPropertiesSet() 方法。
5.4、如果 bean 设置了 init-method 属性,则触发 init-method 指定的方法。
5.5、触发 BeanPostProcessor 的 postProcessAfterInitialization 方法。
6、将创建好的 bean 实例放到缓存中,用于之后使用。
finishRefresh();
使用应用事件广播器推送上下文刷新完毕事件(ContextRefreshedEvent )到相应的监听器。
总结
Srping IOC框架拓展性设计非常好,像AOP、事务、Spring MVC 都是基于IOC基础之上,所以啃好IOC,学习后面的知识会比较容易接收。