1. IOC容器的初始化过程:IOC容器的初始化由refresh()方法启动,这个启动包括:BeanDifinition的Resource定位,加载和注册三个过程。初始化的过程不包含Bean依赖注入的实现。

  • 第一个过程是Resource的定位过程。这个Resource的定位指的是BeanDefinition的资源定位,它由ResourceLoader通过统一的Resource接口完成。
  • 第二个过程是BeanDefinition的载入,这个过程是把用户定义好的Bean表示为容器的内部数据结构(即BeanDefinition)
  • 第三个过程是向IOC容器注册这些BeanDefinition的过程。

2. 下面我们以ClassPathXmlApplicationContext 为例分析这个ApplicationContext的实现,使用的spring版本是3.0.2版本。首先看下我们测试的代码和配置文件:

  • 2.1:javaBean的定义
  1. public class Person {
  2. private String name;
  3. private int age;
  4. private int sex;
  5. private Dog dog;
  6. private List<Address> addressList;
          
  7. }
  8.  
  9. public class Dog {
  10. private String dogName;
  11. }
  12.  
  13. public class Address {
  14. private String type;
  15. private String city;
  16. }
  • 2. 2beans.xml文件定义:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <!-- person对象 -->
  7. <bean id="person" class="com.pepper.spring.model.Person" >
  8. <property name="name" value="pepper" />
  9. <property name="age" value="24" />
  10. <property name="sex" value="1" />
  11. <property name="dog" ref="dog" />
  12. <property name="addressList">
  13. <list>
  14. <ref bean="home"/>
  15. <ref bean="work"/>
  16. </list>
  17. </property>
  18. </bean>
  19.  
  20. <!-- person对象的Dog属性 -->
  21. <bean id="dog" class="com.pepper.spring.model.Dog">
  22. <property name="dogName" value="Edward" />
  23. </bean>
  24.  
  25. <!-- person对象AddressList属性的两个值 -->
  26. <bean id="home" class="com.pepper.spring.model.Address">
  27. <property name="type" value="home" />
  28. <property name="city" value="SX" />
  29. </bean>
  30. <bean id="work" class="com.pepper.spring.model.Address">
  31. <property name="type" value="work" />
  32. <property name="city" value="SZ" />
  33. </bean>
  34. </beans>
  • 2.3. 容器启动类:
  1. public class TestSpring {
  2.  
  3. public static final String BEAN_CONFIG_FILE = "E:/workspace_selflearn/read-spring/src/spring-beans.xml";
  4. public static final String BEAN_CONFIG_CLASS = "spring-beans.xml";
  5.  
  6. public static void main(String[] args) {
  7. testApplicationContext();
  8. }
  9. //使用XmlBeanFactory
  10. public static void testBeanFactory() {
  11. Resource res = new ClassPathResource(BEAN_CONFIG_CLASS);
  12. BeanFactory fac = new XmlBeanFactory(res);
  13. Person p = fac.getBean("person", Person.class);
  14. System.out.println(p);
  15. }
  16.  
  17. //使用ApplicationContext
  18. public static void testApplicationContext() {
  19. ApplicationContext ac = null;
  20. // ac = new FileSystemXmlApplicationContext(BEAN_CONFIG_FILE);
  21. ac = new ClassPathXmlApplicationContext(BEAN_CONFIG_CLASS);
  22. Person p = ac.getBean("person", Person.class);
  23. System.out.println(p);
  24. }
  25. }

3. 我们先看下ClassPathXmlApplicationContext类的定义:

  1. public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {
  2.  
  3. private Resource[] configResources;
  4.  
  5. public ClassPathXmlApplicationContext() {
  6. }
  7.  
  8. public ClassPathXmlApplicationContext(ApplicationContext parent) {
  9. super(parent);
  10. }
  11. // configLocation对象表示BeanDefinition所在的文件路径
  12. public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
  13. this(new String[] {configLocation}, true, null);
  14. }
  15. // Spring支持传入多个BeanDefinition配置文件
  16. public ClassPathXmlApplicationContext(String... configLocations) throws BeansException {
  17. this(configLocations, true, null);
  18. }
  19. // 此构造方法除了包含配置文件路径,还允许指定想要使用的父类IOC容器
  20. public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {
  21. this(configLocations, true, parent);
  22. }
  23.  
  24. public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {
  25. this(configLocations, refresh, null);
  26. }
  27. //对象的初始化过程中,调用refresh()方法启动BeanDefinition的载入过程
  28. public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)throws BeansException {
  29. super(parent);
  30. setConfigLocations(configLocations);
  31. if (refresh) {
  32. refresh();
  33. }
  34. }
  35.  
  36. public ClassPathXmlApplicationContext(String[] paths, Class clazz) throws BeansException {
  37. this(paths, clazz, null);
  38. }
  39.  
  40. public ClassPathXmlApplicationContext(String[] paths, Class clazz, ApplicationContext parent) throws BeansException {
  41.  
  42. super(parent);
  43. Assert.notNull(paths, "Path array must not be null");
  44. Assert.notNull(clazz, "Class argument must not be null");
  45. this.configResources = new Resource[paths.length];
  46. for (int i = 0; i < paths.length; i++) {
  47. this.configResources[i] = new ClassPathResource(paths[i], clazz);
  48. }
  49. refresh();
  50. }
  51. //获取资源配置文件
  52. @Override
  53. protected Resource[] getConfigResources() {
  54. return this.configResources;
  55. }
  56.  
  57. } 

容器启动的时候会调用ClassPathXmlApplicationContext的构造方法,在这个构造方法中会调用refresh()方法,这个方法十分重要,这是我们分析容器初始化过程中至关重要的一个接口,我们后面分析的Bean的载入,解析,注册都是以这个方法作为入口开始的。

  1. public void refresh() throws BeansException, IllegalStateException {
  2. synchronized (this.startupShutdownMonitor) {
  3. // Prepare this context for refreshing.准备上下文
  4. prepareRefresh();
  5.  
  6. // Tell the subclass to refresh the internal bean factory.通知子类刷新内部的bean工厂
  7. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
  8.  
  9. // Prepare the bean factory for use in this context.准备beanfactory来使用这个上下文.做一些准备工作,例如classloader,beanfactoryPostProcessor等
  10. prepareBeanFactory(beanFactory);
  11.  
  12. try {
  13. // Allows post-processing of the bean factory in context subclasses.在子类上下文中允许beanfactory进行后置处理
  14. postProcessBeanFactory(beanFactory);
  15.  
  16. // Invoke factory processors registered as beans in the context.调用工厂处理器作为bean注册到上下文中
  17. invokeBeanFactoryPostProcessors(beanFactory);
  18.  
  19. // Register bean processors that intercept bean creation.开始注册BeanPostProcessor来拦截bean的创建过程
  20. registerBeanPostProcessors(beanFactory);
  21.  
  22. // Initialize message source for this context.为上下文初始化消息源
  23. initMessageSource();
  24.  
  25. // Initialize event multicaster for this context.为上下文初始化事件广播
  26. initApplicationEventMulticaster();
  27.  
  28. // Initialize other special beans in specific context subclasses.在具体的子类上下文中初始化特殊的bean
  29. onRefresh();
  30.  
  31. // Check for listener beans and register them.检查监听器bean并注册
  32. registerListeners();
  33.  
  34. // Instantiate all remaining (non-lazy-init) singletons.实例化所有的剩余的singleton的bean
  35. finishBeanFactoryInitialization(beanFactory);
  36.  
  37. // Last step: publish corresponding event.最后一步,发布应用
  38. finishRefresh();
  39. }
  40.  
  41. catch (BeansException ex) {
  42. // Destroy already created singletons to avoid dangling resources.
  43. destroyBeans();
  44.  
  45. // Reset 'active' flag.
  46. cancelRefresh(ex);
  47.  
  48. // Propagate exception to caller.
  49. throw ex;
  50. }
  51. }
  52. }

  

Spring-IOC源码解读2-容器的初始化过程的更多相关文章

  1. Spring Ioc源码分析系列--容器实例化Bean的四种方法

    Spring Ioc源码分析系列--实例化Bean的几种方法 前言 前面的文章Spring Ioc源码分析系列--Bean实例化过程(二)在讲解到bean真正通过那些方式实例化出来的时候,并没有继续分 ...

  2. Spring IoC源码解读——谈谈bean的几种状态

    阅读Spring IoC部分源码有一段时间了,经过不断的单步调试和参阅资料,对Spring容器中bean管理有了一定的了解.这里从bean的几个状态的角度出发,研究下IoC容器. 一.原材料 Xml中 ...

  3. 精尽Spring MVC源码分析 - WebApplicationContext 容器的初始化

    该系列文档是本人在学习 Spring MVC 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释 Spring MVC 源码分析 GitHub 地址 进行阅读 Spring 版本:5.2. ...

  4. Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析

    Spring Ioc源码分析系列--Ioc容器BeanFactoryPostProcessor后置处理器分析 前言 上一篇文章Spring Ioc源码分析系列--Ioc源码入口分析已经介绍到Ioc容器 ...

  5. Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理

    Spring Ioc源码分析系列--Ioc容器注册BeanPostProcessor后置处理器以及事件消息处理 前言 上一篇分析了BeanFactoryPostProcessor的作用,那么这一篇继续 ...

  6. Spring IOC 源码分析

    Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容器.既然大家平时都要用到 Spring,怎么可以不好好了解 Spring 呢?阅读本文 ...

  7. spring IoC源码分析 (3)Resource解析

    引自 spring IoC源码分析 (3)Resource解析 定义好了Resource之后,看到XmlFactoryBean的构造函数 public XmlBeanFactory(Resource  ...

  8. Spring IoC源码解析之invokeBeanFactoryPostProcessors

    一.Bean工厂的后置处理器 Bean工厂的后置处理器:BeanFactoryPostProcessor(触发时机:bean定义注册之后bean实例化之前)和BeanDefinitionRegistr ...

  9. Spring IoC源码解析之getBean

    一.实例化所有的非懒加载的单实例Bean 从org.springframework.context.support.AbstractApplicationContext#refresh方法开发,进入到 ...

随机推荐

  1. HDU 4044 GeoDefense (树形DP,混合经典)

    题意: 给一棵n个节点的树,点1为敌方基地,叶子结点都为我方阵地.我们可以在每个结点安放炸弹,每点至多放一个,每个结点有ki种炸弹可选,且每种炸弹有一个花费和一个攻击力(1点攻击力使敌人掉1点hp). ...

  2. ArcGis server连接oracle

    ArcGIG server连接Oracle 目录--gis服务器--添加arcgis server 下一步: 身份验证为在arcgis server manager 中的管理员登录密码和账户 对于服务 ...

  3. Jordan 标准型定理

    将学习到什么 就算两个矩阵有相同的特征多项式,它们也有可能不相似,那么如何判断两个矩阵是相似的?答案是它们有一样的 Jordan 标准型.   Jordan 标准型定理 这节目的:证明每个复矩阵都与一 ...

  4. 验证IP端与数据库Ip端是否重复!!!

    select COUNT(id) from house_info_config hic where (hic.ip_start <![CDATA[<=]]> #{ipend} AND ...

  5. LeetCode || 双指针 / 单调栈

    11. Container With Most Water 题意:取两根求最大体积 思路:使用两个指针分别指向头和尾,然后考虑左右两根: 对于小的那根,如果选择了它,那么能够产生的最大体积一定是当前的 ...

  6. var、let、const声明变量的区别

    let和var声明变量的区别:1.let所声明的变量只在let命令所在的代码块内有效.(块级作用域) for(let i=0;i<10;i++){ // ... } console.log(i) ...

  7. SpringMVC+Spring+Mybatis整合程序之整合

    因为每个人思路不一样,所以我在这边先分享自己的思路对于mybatis开发持久层(DAO:DataBase Access Object 持久层访问对象)有两种.第一种:传统的开发持久层方式即需要程序员开 ...

  8. 简单css动画 fadeIn fadeOut flash

    考虑兼容性采用 -webkit- -o- -mos- -ms- @keyframes fadeIn{ 0%{ opacity: 0; display: block; } 100%{ opacity: ...

  9. UVa-101-木块问题

    这题用vector比较好写,我们设置对应的几个函数,然后进行相应的操作来简化代码,这样才不易出错. 对于输入和操作来说我们经分析之后,可以看到最后一个操作时最原始的操作也就是不需要还原任意一个堆任意高 ...

  10. PAT Basic 1024

    1024 科学计数法 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+,即数字的整数部分只有1 ...