1.spring容器初始化的2种方式:
spring容器有2种初始化过程,一个是xml配置文件的方式,一种是注解方式。
下面分别举例这两种方式的用法:
xml方式:
/**
* @author chenhairong3
* @description
* @date 2020/12/29
*/
public class TestSpring {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/spring-config.xml");
TestDao testDao = (TestDao)applicationContext.getBean("testDao");
testDao.insert();
}
}
spring-config.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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:people="http://www.springframework.org/schema/people"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/people
file:///D://idea-workspace//test-spring//test-spring-configuration//src//main//resources//META-INF//people.xsd"
default-autowire="byName">
<bean id="testDao" class="com.chr.test.spring.TestDao">
</bean>
</beans>
输出结果:
TestDao类中的insert()方法被调用=====================
注解方式的使用:
注入bean TestAnnotationDao
/**
* @author chenhairong3
* @description
* @date 2020/12/30
*/
@Component
public class TestAnnotationDao {
public void insert(){
System.out.println("TestDao类中的insert()方法被调用=====================");
}
}
配置类:
/**
* @author chenhairong3
* @description
* @date 2020/12/30
*/
@Configuration
@ComponentScan("com.chr.test.spring")
public class JavaConfig {
}
测试类:
/**
* @author chenhairong3
* @description
* @date 2020/12/30
*/
public class TestAnnotation {
public static void main(String[] args) {
AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(JavaConfig.class);
TestAnnotationDao testAnnotationDao = (TestAnnotationDao)configApplicationContext.getBean("testAnnotationDao");
testAnnotationDao.insert();
configApplicationContext.close();
}
}
输出结果:
2. 源码分析
我用的spring版本是:5.0.7.RELEASE
我们就以xml的方式来分析,注解方式感兴趣的可以自行分析,其实原理都差不多。
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/spring-config.xml");
继续跟踪:
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
super(parent);
this.setConfigLocations(configLocations);
if (refresh) {
//容器刷新的方法
this.refresh();
}
}
进入refresh方法。refresh方法是在AbstractApplicationContext这个类中。无论是xml配置方式还是注解方式refresh方法最终都会掉到这里。
refresh方法里面用到了模板方法设计模式。refresh方法中定义整个容器刷新的步骤。
public void refresh() throws BeansException, IllegalStateException {
synchronized(this.startupShutdownMonitor) {
//第一步:在容器刷新的之前做一些初始化的工作。会做一些日志打印和时间记录等。
this.prepareRefresh();
//第二步:需要重点关注,这里会判断如果已经有beanFactory,则销毁,然后重新创建beanFactory.然后会加载xml中配置的bean,loadBeanDefinition方法就是在这里被调用的。
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
//第三步:给beanFactory设置一些属性,比如setBeanClassLoader类加载器,和一些环境变量属性等。
this.prepareBeanFactory(beanFactory);
try {
//第4步,预留的给子类扩展实现的方法,xml方式的话,此方法没有实现类。
this.postProcessBeanFactory(beanFactory);
//第5步:需要重点关注,开始调用spring的扩展点BeanFactoryPostProcess的方法。
找到所有的beanFactoryPostProcessor对象,执行他的postProcessBeanFactory方法。
this.invokeBeanFactoryPostProcessors(beanFactory);
//第6步:注册beanPostProcessor对象
this.registerBeanPostProcessors(beanFactory);
//第7步:国际化处理
this.initMessageSource();
//第8步:spring的事件监听机制处理
this.initApplicationEventMulticaster();
//第9步:留给子类处理的。在实例化前做一些特定处理。xml方式此方法未用到。
this.onRefresh();
//第10步:注册listener
this.registerListeners();
//第11步:重点关注:实例化和初始化bean信息都在这里完成的。循环依赖的解决等都在这一步处理的。
this.finishBeanFactoryInitialization(beanFactory);
//第12步:初始化后的收尾工作
this.finishRefresh();
} catch (BeansException var9) {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
}
this.destroyBeans();
this.cancelRefresh(var9);
throw var9;
} finally {
this.resetCommonCaches();
}
}
}
上面的步骤中我们需要重点关注第2,5,11步。
鉴于篇幅原因,我会重开3篇文章,重点来分析2,5,11步骤。