爱上源码,重学Spring IoC深入

打印 上一主题 下一主题

主题 971|帖子 971|积分 2913

  1. 回答:
  2. 我们为什么要学习源码?
  3. 1、知其然知其所以然
  4. 2、站在巨人的肩膀上,提高自己的编码水平
  5. 3、应付面试
复制代码
1.1  Spring源码阅读小技巧

1、类层次藏得太深,不要一个类一个类的去看,遇到方法该进就大胆的进
2、更不要一行一行的去看,看核心点,有些方法并不重要,不要跟它纠缠
3、看不懂的先不看,根据语义和返回值能知道这个方法达到了啥目的即可
4、只看核心接口(下面标注了重点的地方)和核心代码,有些地方也许你使用spring以来都没触发过
5、debug跟步走,源码中给大家标注好了,见到 ”===>“ 就进去
​<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="userService" />
</beans><?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="userService" />
</beans>进去之前,下一行打个断点,方便快速回到岔路口
​<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="userService" />
</beans><?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="userService" />
</beans>进去之前,可以先点方法看源码,再debug跟进
6、广度优先,而非深度优先。先沿着主流程走,了解大概,再细化某些方法
7、认命。spring里多少万行的代码,一部书都写不完。只能学关键点
阅读源码目的
加深理解spring的bean加载过程
面试吹牛x
江湖传说,spring的类关系是这样的……

1.2 IoC初始化流程与继承关系
  1. 引言
  2. 在看源码之前需要掌握Spring的继承关系和初始化
复制代码
1)  IoC容器初始化流程

目标:
1、IoC容器初始化过程中到底都做了哪些事情(宏观目标)
2、IoC容器初始化是如何实例化Bean的(划重点,最终目标)
  1. //没有Spring之前我们是这样的
  2. User user=new User();
  3. user.xxx();
  4. //有了Spring之后我们是这样的
  5. <bean id="userService" >
  6. User user= context.getBean("xxx");
  7. user.xxx();
复制代码
IoC流程简化图:
tips:
下面的流转记不住没有关系
在剖析源码的整个过程中,我们一直会拿着这个图和源码对照

初始化:
1、容器环境的初始化
2、Bean工厂的初始化(IoC容器启动首先会销毁旧工厂、旧Bean、创建新的工厂)
读取与定义
读取:通过BeanDefinitonReader读取我们项目中的配置(application.xml)
定义:通过解析xml文件内容,将里面的Bean解析成BeanDefinition(未实例化、未初始化)
实例化与销毁
Bean实例化、初始化(注入)
销毁缓存等
扩展点
事件与多播、后置处理器
复杂的流程关键点:

重点总结:
1、工厂初始化过程
2、解析xml到BeanDefinition,放到map
3、调用后置处理器
4、从map取出进行实例化( ctor.newInstance)
5、实例化后放到一级缓存(工厂)
2) 容器与工厂继承关系

tips:
别紧张,下面的继承记不住没有关系
关注颜色标注的几个就可以
目标:简单理解ioC容器继承关系

继承关系理解:
1、ClassPathXmlApplicationContext最终还是到了 ApplicationContext 接口,同样的,我们也可以使用绿颜色的 FileSystemXmlApplicationContext 和 AnnotationConfigApplicationContext 这两个类完成容器初始化的工作
2、FileSystemXmlApplicationContext 的构造函数需要一个 xml 配置文件在系统中的路径,其他和 ClassPathXmlApplicationContext 基本上一样
3、AnnotationConfigApplicationContext 的构造函数扫描classpath中相关注解的类,主流程一样
课程中我们以最经典的 classpathXml 为例。
Bean工厂继承关系
目标:
ApplicationContext   和  BeanFactory 啥关系?
BeanFactory 和 FactoryBean呢?

总结:
别害怕,上面的继承关系不用刻意去记住它
其实接触到的就最下面这个!
1.3 开始搭建测试项目

四步:
1、新建测试module项目
首先我们在 Spring 源码项目中新增一个测试项目,点击 New -> Module... 创建一个 Gradle 的 Java 项目

2、详细信息


3、设置gradle

4、完善信息

在 build.gradle 中添加对 Spring 源码的依赖:
  1. compile(project(':spring-context'))
复制代码

spring-context 会自动将 spring-core、spring-beans、spring-aop、spring-expression 这几个基础 jar 包带进来。
接着,我们需要在项目中创建一个 bean 和配置文件(application.xml)及启动文件(Main.java)
接口如下:
  1. package com.spring.test.service;
  2. public interface UserService {
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <beans xmlns="http://www.springframework.org/schema/beans"
  5.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  7.         <bean id="userService" />
  8. </beans>public String getName();
  9. }
复制代码
实现类
  1. package com.spring.test.impl;
  2. import com.spring.test.service.UserService;
  3. public class UserServiceImpl implements UserService {
  4. <?xml version="1.0" encoding="UTF-8"?>
  5. <beans xmlns="http://www.springframework.org/schema/beans"
  6.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  8.         <bean id="userService" />
  9. </beans>@Override
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  13.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  14.         <bean id="userService" />
  15. </beans>public String getName() {
  16. <?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans><?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans>return "Hello World";
  27. <?xml version="1.0" encoding="UTF-8"?>
  28. <beans xmlns="http://www.springframework.org/schema/beans"
  29.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  30.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  31.         <bean id="userService" />
  32. </beans>}
  33. }
复制代码
Main代码如下
  1. public class Test {
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.         <bean id="userService" />
  7. </beans>public static void main(String[] args) {
  8. <?xml version="1.0" encoding="UTF-8"?>
  9. <beans xmlns="http://www.springframework.org/schema/beans"
  10.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  12.         <bean id="userService" />
  13. </beans><?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  17.         <bean id="userService" />
  18. </beans>ApplicationContext context =
  19. <?xml version="1.0" encoding="UTF-8"?>
  20. <beans xmlns="http://www.springframework.org/schema/beans"
  21.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  22.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  23.         <bean id="userService" />
  24. </beans><?xml version="1.0" encoding="UTF-8"?>
  25. <beans xmlns="http://www.springframework.org/schema/beans"
  26.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  27.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  28.         <bean id="userService" />
  29. </beans><?xml version="1.0" encoding="UTF-8"?>
  30. <beans xmlns="http://www.springframework.org/schema/beans"
  31.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  32.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  33.         <bean id="userService" />
  34. </beans><?xml version="1.0" encoding="UTF-8"?>
  35. <beans xmlns="http://www.springframework.org/schema/beans"
  36.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  37.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  38.         <bean id="userService" />
  39. </beans>new ClassPathXmlApplicationContext("classpath*:application.xml");
  40. <?xml version="1.0" encoding="UTF-8"?>
  41. <beans xmlns="http://www.springframework.org/schema/beans"
  42.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  43.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  44.         <bean id="userService" />
  45. </beans><?xml version="1.0" encoding="UTF-8"?>
  46. <beans xmlns="http://www.springframework.org/schema/beans"
  47.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  48.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  49.         <bean id="userService" />
  50. </beans>UserService userService = context.getBean(UserService.class);
  51. <?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans><?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans>System.out.println(userService);
  62. <?xml version="1.0" encoding="UTF-8"?>
  63. <beans xmlns="http://www.springframework.org/schema/beans"
  64.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  65.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  66.         <bean id="userService" />
  67. </beans><?xml version="1.0" encoding="UTF-8"?>
  68. <beans xmlns="http://www.springframework.org/schema/beans"
  69.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  70.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  71.         <bean id="userService" />
  72. </beans>// 这句将输出: hello world
  73. <?xml version="1.0" encoding="UTF-8"?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  77.         <bean id="userService" />
  78. </beans><?xml version="1.0" encoding="UTF-8"?>
  79. <beans xmlns="http://www.springframework.org/schema/beans"
  80.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  82.         <bean id="userService" />
  83. </beans>System.out.println(userService.getName());
  84. <?xml version="1.0" encoding="UTF-8"?>
  85. <beans xmlns="http://www.springframework.org/schema/beans"
  86.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  87.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  88.         <bean id="userService" />
  89. </beans>}
  90. }
复制代码
配置文件 application.xml(在 resources 中)配置如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>
复制代码
运行
  1. 输出如下
  2. com.spring.test.impl.UserServiceImpl@2aa5fe93
  3. Hello World
复制代码
1.4 工厂的构建
  1. 引言:
  2. 接下来,我们就正式讲解Spring ioC容器的源码
  3. 我们的目的:看一下ioC如何帮我们生成对象的
复制代码
生命周期
1)ApplicationContext入口

参考 IocTest.java
测试代码:spring支持多种bean定义方式,为方便大家理解结构,以xml为案例,后面的解析流程一致
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans><?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans>ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:${xmlName}.xml");<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans>// (c)从容器中取出Bean的实例,call:AbstractApplicationContext.getBean(java.lang.Class)<?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans><?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans>//工厂模式(simple)<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans><?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans>UserService userService = (UserService) context.getBean("userServiceBeanId");<?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans>// 这句将输出: hello world<?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans><?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans>System.out.println(userService.getName());
复制代码
进入到ClassPathXmlApplicationContext的有参构造器
org.springframework.context.support.ClassPathXmlApplicationContext#ClassPathXmlApplicationContext(java.lang.String[], boolean, org.springframework.context.ApplicationContext)
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>public ClassPathXmlApplicationContext(<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans><?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans>String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)<?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans><?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans><?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans>throws BeansException {<?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans><?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans>//继承结构图<?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans><?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans>//1、返回一个classloader<?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.         <bean id="userService" />
  66. </beans>//2、返回一个解析器<?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.         <bean id="userService" />
  71. </beans><?xml version="1.0" encoding="UTF-8"?>
  72. <beans xmlns="http://www.springframework.org/schema/beans"
  73.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  75.         <bean id="userService" />
  76. </beans>super(parent);<?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.         <bean id="userService" />
  81. </beans><?xml version="1.0" encoding="UTF-8"?>
  82. <beans xmlns="http://www.springframework.org/schema/beans"
  83.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  84.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  85.         <bean id="userService" />
  86. </beans>// 1、获取环境(系统环境、jvm环境)<?xml version="1.0" encoding="UTF-8"?>
  87. <beans xmlns="http://www.springframework.org/schema/beans"
  88.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  89.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  90.         <bean id="userService" />
  91. </beans><?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  95.         <bean id="userService" />
  96. </beans>// 2、设置Placeholder占位符解析器<?xml version="1.0" encoding="UTF-8"?>
  97. <beans xmlns="http://www.springframework.org/schema/beans"
  98.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  99.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  100.         <bean id="userService" />
  101. </beans><?xml version="1.0" encoding="UTF-8"?>
  102. <beans xmlns="http://www.springframework.org/schema/beans"
  103.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  104.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  105.         <bean id="userService" />
  106. </beans>// 2、将xml的路径解析完存储到数组<?xml version="1.0" encoding="UTF-8"?>
  107. <beans xmlns="http://www.springframework.org/schema/beans"
  108.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  109.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  110.         <bean id="userService" />
  111. </beans><?xml version="1.0" encoding="UTF-8"?>
  112. <beans xmlns="http://www.springframework.org/schema/beans"
  113.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  114.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  115.         <bean id="userService" />
  116. </beans>setConfigLocations(configLocations);<?xml version="1.0" encoding="UTF-8"?>
  117. <beans xmlns="http://www.springframework.org/schema/beans"
  118.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  119.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  120.         <bean id="userService" />
  121. </beans><?xml version="1.0" encoding="UTF-8"?>
  122. <beans xmlns="http://www.springframework.org/schema/beans"
  123.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  124.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  125.         <bean id="userService" />
  126. </beans>//默认为true<?xml version="1.0" encoding="UTF-8"?>
  127. <beans xmlns="http://www.springframework.org/schema/beans"
  128.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  129.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  130.         <bean id="userService" />
  131. </beans><?xml version="1.0" encoding="UTF-8"?>
  132. <beans xmlns="http://www.springframework.org/schema/beans"
  133.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  134.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  135.         <bean id="userService" />
  136. </beans>if (refresh) {<?xml version="1.0" encoding="UTF-8"?>
  137. <beans xmlns="http://www.springframework.org/schema/beans"
  138.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  139.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  140.         <bean id="userService" />
  141. </beans><?xml version="1.0" encoding="UTF-8"?>
  142. <beans xmlns="http://www.springframework.org/schema/beans"
  143.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  144.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  145.         <bean id="userService" />
  146. </beans><?xml version="1.0" encoding="UTF-8"?>
  147. <beans xmlns="http://www.springframework.org/schema/beans"
  148.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  149.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  150.         <bean id="userService" />
  151. </beans>//核心方法(模板)<?xml version="1.0" encoding="UTF-8"?>
  152. <beans xmlns="http://www.springframework.org/schema/beans"
  153.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  154.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  155.         <bean id="userService" />
  156. </beans><?xml version="1.0" encoding="UTF-8"?>
  157. <beans xmlns="http://www.springframework.org/schema/beans"
  158.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  159.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  160.         <bean id="userService" />
  161. </beans><?xml version="1.0" encoding="UTF-8"?>
  162. <beans xmlns="http://www.springframework.org/schema/beans"
  163.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  164.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  165.         <bean id="userService" />
  166. </beans>refresh();<?xml version="1.0" encoding="UTF-8"?>
  167. <beans xmlns="http://www.springframework.org/schema/beans"
  168.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  169.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  170.         <bean id="userService" />
  171. </beans><?xml version="1.0" encoding="UTF-8"?>
  172. <beans xmlns="http://www.springframework.org/schema/beans"
  173.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  174.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  175.         <bean id="userService" />
  176. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  177. <beans xmlns="http://www.springframework.org/schema/beans"
  178.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  179.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  180.         <bean id="userService" />
  181. </beans>}
复制代码
重点步骤解析(断点跟踪讲解)
  1. super方法做了哪些事情
  2. 1、super方法:通过点查看父容器与子容器概念
  3. 2、super方法:调用到顶端,一共5层,每一层都要与讲义中的【ioC与Bean工厂类关系继承】进行对照
  4. 3、super方法:在什么地方初始化的类加载器和解析器
  5. setConfigLocations方法做了哪些事情:
  6. 1、如何返回的系统环境和jvm环境
  7. 2、路径的解析
  8. 3、设置占位符解析器
  9. 进入核心方法refresh
复制代码
2)预刷新

prepareRefresh()【准备刷新】
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans><?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans>// synchronized块锁(monitorenter --monitorexit),不然 refresh() 还没结束,又来个启动或销毁容器的操作<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans>synchronized (this.startupShutdownMonitor) {<?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans><?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans><?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans>//1、【准备刷新】【Did four things】<?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans><?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans>prepareRefresh();<?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans><?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.         <bean id="userService" />
  66. </beans><?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.         <bean id="userService" />
  71. </beans><?xml version="1.0" encoding="UTF-8"?>
  72. <beans xmlns="http://www.springframework.org/schema/beans"
  73.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  75.         <bean id="userService" />
  76. </beans><?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.         <bean id="userService" />
  81. </beans><?xml version="1.0" encoding="UTF-8"?>
  82. <beans xmlns="http://www.springframework.org/schema/beans"
  83.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  84.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  85.         <bean id="userService" />
  86. </beans>......。略
复制代码
讲解重点(断点跟踪、类继承关系、架构图讲解)
  1. prepareRefresh干了哪些事情<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>//1、记录启动时间/设置开始标志<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans>//2、子类属性扩展(模板方法)<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>//3、校验xml配置文件<?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans>//4、初始化早期发布的应用程序事件对象(不重要,仅仅是创建setg对象)
复制代码
3)创建bean工厂【重点】

【获得新的bean工厂】obtainFreshBeanFactory()
最终目的就是解析xml,注册bean定义
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans><?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans><?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>关键步骤<?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans><?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans><?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans>//1、关闭旧的 BeanFactory<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans><?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans><?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans>//2、创建新的 BeanFactory(DefaluListbaleBeanFactory)<?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans><?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans><?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans>//3、解析xml/加载 Bean 定义、注册 Bean定义到beanFactory(未初始化)<?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.         <bean id="userService" />
  66. </beans><?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.         <bean id="userService" />
  71. </beans><?xml version="1.0" encoding="UTF-8"?>
  72. <beans xmlns="http://www.springframework.org/schema/beans"
  73.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  75.         <bean id="userService" />
  76. </beans>//4、返回全新的工厂<?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.         <bean id="userService" />
  81. </beans><?xml version="1.0" encoding="UTF-8"?>
  82. <beans xmlns="http://www.springframework.org/schema/beans"
  83.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  84.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  85.         <bean id="userService" />
  86. </beans><?xml version="1.0" encoding="UTF-8"?>
  87. <beans xmlns="http://www.springframework.org/schema/beans"
  88.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  89.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  90.         <bean id="userService" />
  91. </beans>ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
复制代码
4)bean工厂前置操作

【准备bean工厂】prepareBeanFactory(beanFactory);
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>//1、设置 BeanFactory 的类加载器<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans>//2、设置 BeanFactory 的表达式解析器<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>//3、设置 BeanFactory 的属性编辑器<?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans>//4、智能注册
复制代码
tips
当前代码逻辑简单、且非核心
5)bean工厂后置操作

【后置处理器Bean工厂】postProcessBeanFactory(beanFactory)    空方法
tips:子类实现
空方法,跳过
6)工厂后置处理器【重点】

【调用bean工厂后置处理器】invokeBeanFactoryPostProcessors(beanFactory);
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>//调用顺序一:bean定义注册后置处理器<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans>//调用顺序二:bean工厂后置处理器<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>PostProcessorRegistrationDelegate 类里有详细注解
复制代码
tips
invoke方法近200行
关注两类后置处理器的方法执行步骤和顺序
7)bean后置处理器

【注册bean后置处理器】registerBeanPostProcessors(beanFactory)
  1. //6、【注册bean后置处理器】只是注册,但是不会反射调用
  2. //功能:找出所有实现BeanPostProcessor接口的类,分类、排序、注册
  3. registerBeanPostProcessors(beanFactory);
复制代码
  1. //<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>核心:查看重要的3步;最终目的都是实现bean后置处理器的注册// 第一步: implement PriorityOrdered// 第二步: implement Ordered.// 第三步: Register all internal BeanPostProcessors.
复制代码
8)国际化

【初始化消息源】国际化问题i18n   initMessageSource();
tips:
就加了个bean进去,非核心步骤,跳过
9)初始化事件广播器

【初始化应用程序事件多路广播】initApplicationEventMulticaster();
tips:
需要讲解观察者设计模式
重点:就放了个bean进去, 到下面的 listener再联调。
10)刷新

【刷新】 onRefresh();
空的,交给子类实现:默认情况下不执行任何操作
  1. // 具体的子类可以在这里初始化一些特殊的 Bean(在初始化 singleton beans 之前)
  2. onRefresh();
  3. 不重要,跳过
复制代码
11)注册监听器【重点】

【注册所有监听器】registerListeners();
测试代码参考:MulticastTest
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>//获取所有实现了ApplicationListener,然后进行注册<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans>//1、集合applicationListeners查找<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>//2、bean工厂找到实现ApplicationListener接口的bean<?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans>//3、this.earlyApplicationEvents;
复制代码
tips:
需要讲解观察者设计模式
重点:演示多播和容器发布
12)完成bean工厂【重点】

【完成bean工厂初始化操作】finishBeanFactoryInitialization(beanFactory);
  1. //【完成bean工厂初始化操作】负责初始化所有的 singleton beans
  2. //此处开始调用Bean的前置处理器和后置处理器
  3. finishBeanFactoryInitialization(beanFactory);
复制代码
讲解重点(断点跟踪、类继承关系、架构图讲解)
  1.   //1、设置辅助器:例如:解析器、转换器、类装载器<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>//2、实例化<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans>//3、填充<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>//4、调用前置、后置处理器
复制代码
  1. //核心代码在  getBean() , 下面单独讲解
复制代码
13)完成刷新

【完成刷新】
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>protected void finishRefresh() {<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans><?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>// 1、清除上下文级资源缓存<?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans><?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans>clearResourceCaches();<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans><?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans>// 2、LifecycleProcessor接口初始化<?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans><?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans>// ps:当ApplicationContext启动或停止时,它会通过LifecycleProcessor来与所有声明的bean的周期做状态更新<?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans><?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans>// 而在LifecycleProcessor的使用前首先需要初始化<?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.         <bean id="userService" />
  66. </beans>initLifecycleProcessor();<?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.         <bean id="userService" />
  71. </beans><?xml version="1.0" encoding="UTF-8"?>
  72. <beans xmlns="http://www.springframework.org/schema/beans"
  73.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  75.         <bean id="userService" />
  76. </beans>// 3、启动所有实现了LifecycleProcessor接口的bean<?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.         <bean id="userService" />
  81. </beans><?xml version="1.0" encoding="UTF-8"?>
  82. <beans xmlns="http://www.springframework.org/schema/beans"
  83.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  84.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  85.         <bean id="userService" />
  86. </beans>//DefaultLifecycleProcessor,默认实现<?xml version="1.0" encoding="UTF-8"?>
  87. <beans xmlns="http://www.springframework.org/schema/beans"
  88.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  89.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  90.         <bean id="userService" />
  91. </beans><?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  95.         <bean id="userService" />
  96. </beans>getLifecycleProcessor().onRefresh();<?xml version="1.0" encoding="UTF-8"?>
  97. <beans xmlns="http://www.springframework.org/schema/beans"
  98.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  99.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  100.         <bean id="userService" />
  101. </beans><?xml version="1.0" encoding="UTF-8"?>
  102. <beans xmlns="http://www.springframework.org/schema/beans"
  103.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  104.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  105.         <bean id="userService" />
  106. </beans>// 4、发布上下文刷新完毕事件到相应的监听器<?xml version="1.0" encoding="UTF-8"?>
  107. <beans xmlns="http://www.springframework.org/schema/beans"
  108.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  109.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  110.         <bean id="userService" />
  111. </beans><?xml version="1.0" encoding="UTF-8"?>
  112. <beans xmlns="http://www.springframework.org/schema/beans"
  113.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  114.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  115.         <bean id="userService" />
  116. </beans>//ps:当完成容器初始化的时候,<?xml version="1.0" encoding="UTF-8"?>
  117. <beans xmlns="http://www.springframework.org/schema/beans"
  118.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  119.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  120.         <bean id="userService" />
  121. </beans><?xml version="1.0" encoding="UTF-8"?>
  122. <beans xmlns="http://www.springframework.org/schema/beans"
  123.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  124.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  125.         <bean id="userService" />
  126. </beans>// 要通过Spring中的事件发布机制来发出ContextRefreshedEvent事件,以保证对应的监听器可以做进一步的逻辑处理<?xml version="1.0" encoding="UTF-8"?>
  127. <beans xmlns="http://www.springframework.org/schema/beans"
  128.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  129.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  130.         <bean id="userService" />
  131. </beans><?xml version="1.0" encoding="UTF-8"?>
  132. <beans xmlns="http://www.springframework.org/schema/beans"
  133.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  134.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  135.         <bean id="userService" />
  136. </beans>publishEvent(new ContextRefreshedEvent(this));<?xml version="1.0" encoding="UTF-8"?>
  137. <beans xmlns="http://www.springframework.org/schema/beans"
  138.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  139.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  140.         <bean id="userService" />
  141. </beans><?xml version="1.0" encoding="UTF-8"?>
  142. <beans xmlns="http://www.springframework.org/schema/beans"
  143.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  144.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  145.         <bean id="userService" />
  146. </beans>// 5、把当前容器注册到到MBeanServer,用于jmx使用<?xml version="1.0" encoding="UTF-8"?>
  147. <beans xmlns="http://www.springframework.org/schema/beans"
  148.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  149.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  150.         <bean id="userService" />
  151. </beans><?xml version="1.0" encoding="UTF-8"?>
  152. <beans xmlns="http://www.springframework.org/schema/beans"
  153.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  154.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  155.         <bean id="userService" />
  156. </beans>LiveBeansView.registerApplicationContext(this);<?xml version="1.0" encoding="UTF-8"?>
  157. <beans xmlns="http://www.springframework.org/schema/beans"
  158.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  159.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  160.         <bean id="userService" />
  161. </beans>}
复制代码
tips:
非核心步骤
2 singleton bean 创建【重点】

下面拎出来,重点讲 getBean方法。
参考代码:
先看没有循环依赖的情况,普通单例bean的初始化   SinigleTest.java
后面再讲循环依赖
1)调用入口

大家都知道是getBean()方法,但是这个方法要注意,有很多调用时机
如果你把断点打在了这里,再点进去getBean,你将会直接从singleton集合中拿到一个实例化好的bean
无法看到它的实例化过程。
可以debug试一下。会发现直接从getSingleTon返回了bean,这不是我们想要的模样……

思考一下,为什么呢?
回顾 1.4中的第 12 小节,在bean工厂完成后,会对singleton的bean完成初始化,那么真正的初始化应该发生在那里!
那就需要找到:DefaultListableBeanFactory的第 809 行,那里的getBean
也可以从 1.4的第12小节的入口跟进去。断点打在这里试试:

这也是我们在上面留下的尾巴。
本小节我们从这里继续……
2)主流程

小tip:先搞清除3级缓存的事
关于bean的三级缓存:DefaultSingletonBeanRegistry代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>/**<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans> * 一级缓存:单例(对象)池,这里面的对象都是确保初始化完成,可以被正常使用的<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans> * 它可能来自3级,或者2级<?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans> */<?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans>private final Map singletonObjects = new ConcurrentHashMap(256);<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans>/**<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans> * 三级缓存:单例工厂池,这里面不是bean本身,是它的一个工厂,未来调getObject来获取真正的bean<?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans> * 一旦获取,就从这里删掉,进入2级(发生闭环的话)或1级(没有闭环)<?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans> */<?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans>private final Map></p></blockquote>[b]变化过程3-3:如下图:[/b]
  52. [img]https://img2022.cnblogs.com/other/2893049/202210/2893049-20221026150702299-1554045943.png[/img]
  53. [indent]步骤:
  54. 此时, BeanB里面已经注入了BeanA,它自己完成并进入了一级缓存
  55. 要注意,它的完成是被动的结果,也就是A需要它,临时先腾出时间创建了它
  56. 接下来,BeanA 还要继续自己的流程,然后populateBean方法将BeanB注入到自己里
  57. 最后,BeanA 进一级缓存,删除之前的二级
  58. 整个流程完成!
  59. [/indent][b]大功告成:双方相互持有对方效果如下:[/b]
  60. [img]https://img2022.cnblogs.com/other/2893049/202210/2893049-20221026150703512-1723493531.png[/img]
  61. [size=3]2)三级缓存解决方案总结[/size]
  62. 简化版
  63. [img]https://img2022.cnblogs.com/other/2893049/202210/2893049-20221026150703903-145488494.png[/img]
  64. 序列图
  65. [img]https://img2022.cnblogs.com/other/2893049/202210/2893049-20221026150704317-2067633481.png[/img]
  66. [b]三级缓存解决循环依赖过程(回顾)[/b]
  67. 1、BeanA经过gdcd方法、放入到3级缓存、如果有循环依赖BeanB,重复执行gdcd方法
  68. 2、直到发现了它也需要A,而A前面经历了一次get操作,将3级缓存的BeanA放到2级缓存
  69. 3、然后2级缓存的A注入进BeanB,  BeanB完事进一级缓存,此时BeanB持有BeanA
  70. 3、接下来,继续完成BeanA剩下的操作,取BeanB填充进BeanA,将BeanA放到一级缓存,完成!
  71. [b]伪代码,循环依赖流程一览,都是关键步骤,不能再简化了[/b]
  72. 建议粘贴到vscode等编辑器里查看,因为……它层级太tmd深了!
  73. [code]getBean("A"){<?xml version="1.0" encoding="UTF-8"?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  77.         <bean id="userService" />
  78. </beans>doGetBean("A"){    a = getSingleton("A"){      a = singletonObjects(); //查1级缓存,null      if("创建过3级缓存"){  //不成立        //忽略      }      return a;    }; // A第一次,null        if(a == null){      a = getSingleton("A" , ObjectFactory of){                        a = of.getObject() -> { //lambda表达式          createBean("A"){            doCreateBean("A"){              createBeanInstance("A"); // A 实例化              addSingletonFactory("A"); // A 放入3级缓存              populateBean("A"){                //A 需要B,进入B的getBean                b = getBean("B"){                  doGetBean("B"){                    b = getSingleton("B"); // B第一次,null                    if(b == null){                      b = getSingleton("B", ObjectFactory of){                        b = of.getObject() -> {                          createBean("B"){                            doCreateBean("B"){                              createBeanInstance("B"); // B 实例化                              addSingletonFactory("B"); // B 放入3级缓存                              populateBean("B"){                                //B 需要A,2次进入A的getBean                                a = getBean("A"){                                  doGetBean("A"){                                    a = getSingleton("A"){                                      a = singletonObjects(); //查1级缓存,null                                      if("创建过3级缓存"){  //成立!                                        a = singletonFactory.getObject("A"); //取3级缓存,生成a                                        earlySingletonObjects.put("A", a); //放入2级缓存                                        singletonFactories.remove("A"); //移除3级缓存                                        return a;                                      }                                    }; // A第二次,不是null,但是半成品,还待在2级缓存里                                  } // end doGetBean("A")                                } // end getBean("A")                              }  // end populate B                              initializeBean("B",b); // B后置处理器                            } // end doCreateBean B                          } // end createBean B                        } // end lambda B                        // B 创建完成,并且是完整的,虽然它里面的A还是半成品,但不影响它进入1级                                       addSingleton("B",b) ; // 清除3级缓存,进入1级                      ); // end getSingleton("B",factory)                                     } // end if(b==null);                    return b;                  } // end doGetBean("B")                } // end getBean("B")              } // end populateBean("A")              initializeBean("A"); // A 后置处理器            } //end doCreateBean("A")          } //end crateBean("A")      <?xml version="1.0" encoding="UTF-8"?>
  79. <beans xmlns="http://www.springframework.org/schema/beans"
  80.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  81.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  82.         <bean id="userService" />
  83. </beans>} // end lambda A                addSingleton("A" , a) // 清除2、3级,放入1级      } // end getSingleton("A",factory)      } // end if(a == null)        return a;        } //end doGetBean("A")}//end getBean("A")
复制代码
总结
可以发现,通过spring的三级缓存完美解决了循环依赖
Spring处理机制很聪明;它先扫描一遍Bean,先放到一个容器(3级缓存待命)
此时也不知道是否存在循环依赖,先放到三级缓存再说
等到设置属性的时候,取对应的属性bean去(此时才发现有了循环依赖) ,在放到第二个容器(2级缓存,半成品)
继续,然后从二级缓存拿出进行填充(注入)
填充完毕,将自己放到一级缓存(这个bean是被动创建出来的,因为别人需要它,结果它先完成了)
然后不断循环外层,处理最原始要创建的那个bean
为什么设计三级?二级缓存能否解决循环依赖?
可以解决。别说2级,1级都行
虽然二级缓存能解决循环依赖,但是aop时会可能会引发问题,三级是一个factory,在里面配备了对应的后置处理器,其中就有我们的aop (后面会讲到),如果有人要用它,会在调用factory的getObject时生效,生成代理bean而不是原始bean。
如果不这么做,直接创建原始对象注入,可能引发aop失效。
所以spring的3级各有意义:
1级:最终成品
2级:半成品
3级:工厂,备用
在上面的方法getEarlyBeanReference(提前暴露的引用)
回顾下
AbstractAutowireCapableBeanFactory.getEarlyBeanReference
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans><?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans>if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans><?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans>//循环所有Bean后置处理器<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans><?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans><?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans>for (BeanPostProcessor bp : getBeanPostProcessors()) {<?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans><?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans><?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans>if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {<?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.         <bean id="userService" />
  66. </beans><?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.         <bean id="userService" />
  71. </beans><?xml version="1.0" encoding="UTF-8"?>
  72. <beans xmlns="http://www.springframework.org/schema/beans"
  73.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  75.         <bean id="userService" />
  76. </beans><?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.         <bean id="userService" />
  81. </beans><?xml version="1.0" encoding="UTF-8"?>
  82. <beans xmlns="http://www.springframework.org/schema/beans"
  83.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  84.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  85.         <bean id="userService" />
  86. </beans>SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;<?xml version="1.0" encoding="UTF-8"?>
  87. <beans xmlns="http://www.springframework.org/schema/beans"
  88.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  89.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  90.         <bean id="userService" />
  91. </beans><?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  95.         <bean id="userService" />
  96. </beans><?xml version="1.0" encoding="UTF-8"?>
  97. <beans xmlns="http://www.springframework.org/schema/beans"
  98.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  99.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  100.         <bean id="userService" />
  101. </beans><?xml version="1.0" encoding="UTF-8"?>
  102. <beans xmlns="http://www.springframework.org/schema/beans"
  103.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  104.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  105.         <bean id="userService" />
  106. </beans>  //重点:开始创建AOP代理<?xml version="1.0" encoding="UTF-8"?>
  107. <beans xmlns="http://www.springframework.org/schema/beans"
  108.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  109.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  110.         <bean id="userService" />
  111. </beans><?xml version="1.0" encoding="UTF-8"?>
  112. <beans xmlns="http://www.springframework.org/schema/beans"
  113.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  114.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  115.         <bean id="userService" />
  116. </beans><?xml version="1.0" encoding="UTF-8"?>
  117. <beans xmlns="http://www.springframework.org/schema/beans"
  118.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  119.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  120.         <bean id="userService" />
  121. </beans><?xml version="1.0" encoding="UTF-8"?>
  122. <beans xmlns="http://www.springframework.org/schema/beans"
  123.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  124.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  125.         <bean id="userService" />
  126. </beans><?xml version="1.0" encoding="UTF-8"?>
  127. <beans xmlns="http://www.springframework.org/schema/beans"
  128.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  129.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  130.         <bean id="userService" />
  131. </beans>exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);<?xml version="1.0" encoding="UTF-8"?>
  132. <beans xmlns="http://www.springframework.org/schema/beans"
  133.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  134.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  135.         <bean id="userService" />
  136. </beans><?xml version="1.0" encoding="UTF-8"?>
  137. <beans xmlns="http://www.springframework.org/schema/beans"
  138.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  139.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  140.         <bean id="userService" />
  141. </beans><?xml version="1.0" encoding="UTF-8"?>
  142. <beans xmlns="http://www.springframework.org/schema/beans"
  143.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  144.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  145.         <bean id="userService" />
  146. </beans><?xml version="1.0" encoding="UTF-8"?>
  147. <beans xmlns="http://www.springframework.org/schema/beans"
  148.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  149.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  150.         <bean id="userService" />
  151. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  152. <beans xmlns="http://www.springframework.org/schema/beans"
  153.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  154.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  155.         <bean id="userService" />
  156. </beans><?xml version="1.0" encoding="UTF-8"?>
  157. <beans xmlns="http://www.springframework.org/schema/beans"
  158.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  159.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  160.         <bean id="userService" />
  161. </beans><?xml version="1.0" encoding="UTF-8"?>
  162. <beans xmlns="http://www.springframework.org/schema/beans"
  163.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  164.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  165.         <bean id="userService" />
  166. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  167. <beans xmlns="http://www.springframework.org/schema/beans"
  168.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  169.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  170.         <bean id="userService" />
  171. </beans><?xml version="1.0" encoding="UTF-8"?>
  172. <beans xmlns="http://www.springframework.org/schema/beans"
  173.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  174.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  175.         <bean id="userService" />
  176. </beans>}
复制代码
总结下:
1、如果不调用后置处理器,返回的Bean和三级缓存一样,都是实例化、普通的Bean
2、如果调用后置,返回的就是代理对象,不是普通的Bean了
其实;这就是三级缓存设计的巧妙之处
那为什么要2级呢? 不能直接放入1级吗?
不能!
A-B-A中,第二次A的时候,A还是个半成品,不能放入1级
以上面为例,A在进入2级缓存的时候,它里面的B还是个null !
如果放入1级,被其他使用的地方取走,会引发问题,比如空指针
4 IoC用到的那些设计模式
  1. //啥也没干,调下面传了个true
  2. public Object getSingleton(String beanName)
  3.   
  4. //从1级缓存拿,1级没有再看情况
  5. //后面的参数如果true,就使用3级升2级返回,否则直接返回null
  6. protected Object getSingleton(String beanName, boolean allowEarlyReference)
  7. //1级没有,通过给的factory创建并放入1级里,清除2、3
  8. public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory)
复制代码
4.1 工厂

工厂模式(Factory Pattern)提供了一种创建对象的最佳方式。
工厂模式(Factory Pattern)分为三种
1、简单工厂
2、工厂方法
3、抽象工厂
1. 简单工厂模式
  1. ApplicationContext context =<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>new ClassPathXmlApplicationContext("classpath*:application.xml");\UserService userService = context.getBean(UserService.class);
复制代码
简单工厂模式对对象创建管理方式最为简单,因为其仅仅简单的对不同类对象的创建进行了一层简单的封装
定义接口IPhone
  1. public interface Phone {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>void make();}
复制代码
实现类
  1. public class IPhone implements Phone {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>public IPhone() {<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans><?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>this.make();<?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans>@Override<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans>public void make() {<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans><?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans>// TODO Auto-generated method stub<?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans>System.out.println("生产苹果手机!");<?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans>}}
复制代码
实现类
  1. public class MiPhone implements Phone {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>public MiPhone() {<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans><?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>this.make();<?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans>@Override<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans>public void make() {<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans><?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans>// TODO Auto-generated method stub<?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans>System.out.println("生产小米手机!");<?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans>}}
复制代码
定义工厂类并且测试
  1. public class PhoneFactory {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>public Phone makePhone(String phoneType) {<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans><?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>if (phoneType.equalsIgnoreCase("MiPhone")) {<?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans><?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans><?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans>return new MiPhone();<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans><?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans>} else if (phoneType.equalsIgnoreCase("iPhone")) {<?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans><?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans>return new IPhone();<?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.         <bean id="userService" />
  66. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.         <bean id="userService" />
  71. </beans><?xml version="1.0" encoding="UTF-8"?>
  72. <beans xmlns="http://www.springframework.org/schema/beans"
  73.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  75.         <bean id="userService" />
  76. </beans>return null;<?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.         <bean id="userService" />
  81. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  82. <beans xmlns="http://www.springframework.org/schema/beans"
  83.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  84.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  85.         <bean id="userService" />
  86. </beans>//测试简单工厂<?xml version="1.0" encoding="UTF-8"?>
  87. <beans xmlns="http://www.springframework.org/schema/beans"
  88.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  89.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  90.         <bean id="userService" />
  91. </beans>public static void main(String[] arg) {<?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  95.         <bean id="userService" />
  96. </beans><?xml version="1.0" encoding="UTF-8"?>
  97. <beans xmlns="http://www.springframework.org/schema/beans"
  98.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  99.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  100.         <bean id="userService" />
  101. </beans>PhoneFactory factory = new PhoneFactory();<?xml version="1.0" encoding="UTF-8"?>
  102. <beans xmlns="http://www.springframework.org/schema/beans"
  103.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  104.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  105.         <bean id="userService" />
  106. </beans><?xml version="1.0" encoding="UTF-8"?>
  107. <beans xmlns="http://www.springframework.org/schema/beans"
  108.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  109.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  110.         <bean id="userService" />
  111. </beans>Phone miPhone = factory.makePhone("MiPhone");             <?xml version="1.0" encoding="UTF-8"?>
  112. <beans xmlns="http://www.springframework.org/schema/beans"
  113.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  114.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  115.         <bean id="userService" />
  116. </beans><?xml version="1.0" encoding="UTF-8"?>
  117. <beans xmlns="http://www.springframework.org/schema/beans"
  118.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  119.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  120.         <bean id="userService" />
  121. </beans>IPhone iPhone = (IPhone) factory.makePhone("iPhone");     <?xml version="1.0" encoding="UTF-8"?>
  122. <beans xmlns="http://www.springframework.org/schema/beans"
  123.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  124.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  125.         <bean id="userService" />
  126. </beans>}}
复制代码

4.2  模板

模板模式(Template Pattern):基于抽象类的,核心是封装算法
  1. 引言
  2. 在上面,我们剖析了bean实例化的整个过程
  3. 也就是我们的Bean他是单独存在的,和其他Bean没有交集和引用
  4. 而我们在业务开发中,肯定会有多个Bean相互引用的情况
  5. 也就是所谓的循环依赖
复制代码
模板设计模式—
模板方法定义了一个算法的步骤,并允许子类为一个或多个步骤提供具体实现
  1. //模板模式public abstract class TemplatePattern {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>protected abstract void step1();<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans>protected abstract void step2();<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>protected abstract void step3();<?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans>protected abstract void step4();<?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans>//模板方法<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans>public final void refresh() {<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans>//此处也可加入当前类的一个方法实现,例如init()<?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans><?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans>step1();<?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans><?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans>step2();<?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.         <bean id="userService" />
  66. </beans>step3();<?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.         <bean id="userService" />
  71. </beans><?xml version="1.0" encoding="UTF-8"?>
  72. <beans xmlns="http://www.springframework.org/schema/beans"
  73.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  75.         <bean id="userService" />
  76. </beans>step4();<?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.         <bean id="userService" />
  81. </beans>}}
复制代码
定义子类
  1. //模板模式public class SubTemplatePattern extends TemplatePattern {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>@Override<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans>public void step1() {<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans>System.out.println(">>>>>>>>>>>>>>1");<?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans>@Override<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans>public void step2() {<?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans><?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans>System.out.println(">>>>>>>>>>>>>>2");<?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans>@Override<?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans>public void step3() {<?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.         <bean id="userService" />
  66. </beans><?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.         <bean id="userService" />
  71. </beans>System.out.println(">>>>>>>>>>>>>>3");<?xml version="1.0" encoding="UTF-8"?>
  72. <beans xmlns="http://www.springframework.org/schema/beans"
  73.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  75.         <bean id="userService" />
  76. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.         <bean id="userService" />
  81. </beans>@Override<?xml version="1.0" encoding="UTF-8"?>
  82. <beans xmlns="http://www.springframework.org/schema/beans"
  83.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  84.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  85.         <bean id="userService" />
  86. </beans>public void step4() {<?xml version="1.0" encoding="UTF-8"?>
  87. <beans xmlns="http://www.springframework.org/schema/beans"
  88.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  89.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  90.         <bean id="userService" />
  91. </beans><?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  95.         <bean id="userService" />
  96. </beans>System.out.println(">>>>>>>>>>>>>>4");<?xml version="1.0" encoding="UTF-8"?>
  97. <beans xmlns="http://www.springframework.org/schema/beans"
  98.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  99.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  100.         <bean id="userService" />
  101. </beans>}        //测试<?xml version="1.0" encoding="UTF-8"?>
  102. <beans xmlns="http://www.springframework.org/schema/beans"
  103.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  104.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  105.         <bean id="userService" />
  106. </beans>public static void main(String[] args) {<?xml version="1.0" encoding="UTF-8"?>
  107. <beans xmlns="http://www.springframework.org/schema/beans"
  108.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  109.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  110.         <bean id="userService" />
  111. </beans><?xml version="1.0" encoding="UTF-8"?>
  112. <beans xmlns="http://www.springframework.org/schema/beans"
  113.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  114.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  115.         <bean id="userService" />
  116. </beans>TemplatePattern tp = new SubTemplatePattern();<?xml version="1.0" encoding="UTF-8"?>
  117. <beans xmlns="http://www.springframework.org/schema/beans"
  118.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  119.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  120.         <bean id="userService" />
  121. </beans><?xml version="1.0" encoding="UTF-8"?>
  122. <beans xmlns="http://www.springframework.org/schema/beans"
  123.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  124.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  125.         <bean id="userService" />
  126. </beans>tp.refresh();<?xml version="1.0" encoding="UTF-8"?>
  127. <beans xmlns="http://www.springframework.org/schema/beans"
  128.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  129.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  130.         <bean id="userService" />
  131. </beans>}}
复制代码
输出

4.3 观察者

什么是观察者模式
观察者模式(Observer Pattern):当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知依赖它的对象。
Spring 的事件机制就是具体的观察者模式的实现
  1. //实现类
  2. public class UserServiceImplB implements UserService {
  3.         private UserServiceImplA userServiceImplA;
  4.         public void setUserServiceImplA(UserServiceImplA userServiceImplA) {
  5.                 this.userServiceImplA = userServiceImplA;
  6.         }
  7.         @Override
  8.         public String getName() {
  9.                 return "在UserServiceImplB的Bean中" +
  10.                                 "userServiceImplA注入成功>>>>>>>>>"+userServiceImplA;
  11.         }
复制代码
观察者模式有哪些角色?
事件 ApplicationEvent 是所有事件对象的父类,继承JDK的EventObject
事件监听 ApplicationListener,也就是观察者对象,继承自 JDK 的 EventListener,可以监听到事件;该类中只有一个方法 onApplicationEvent。当监听的事件发生后该方法会被执行。
事件发布ApplicationContext, 实现事件的发布。
(发布事件)
or=========
Spring中的多播
事件发布 ApplicationEventMulticaster,用于事件监听器的注册和事件的广播。

自定义一个事件MessageSourceEvent并且实现ApplicationEvent接口
  1. //在Spring 中使用事件监听机制(事件、监听、发布)//定义事件//执行顺序//1、进入到事件源的有参数构造器//2、发布事件//3、进入到监听器类---one//4、进入到事件源的方法//5、进入到监听器类---two//6、进入到事件源的方法public class MessageSourceEvent extends ApplicationEvent {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>public MessageSourceEvent(Object source) {<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans><?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>super(source);<?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans><?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans>System.out.println("进入到事件源的有参数构造器");<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans>public void print() {<?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans><?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans>System.out.println("进入到事件源的方法");<?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans>}}
复制代码
有了事件之后还需要自定义一个监听用来接收监听到事件,自定义ApplicationContextListener监听 需要交给Spring容器管理, 实现ApplicationListener接口并且重写onApplicationEvent方法,
监听一
  1. //在Spring 中使用事件监听机制(事件、监听、发布)//监听类,在spring配置文件中,注册事件类和监听类public class ApplicationContextListener implements ApplicationListener {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>@Override<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans>public void onApplicationEvent(ApplicationEvent event) {<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans>if (event instanceof MessageSourceEvent) {<?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans><?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans><?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans>System.out.println("进入到监听器类---one");<?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans><?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans>MessageSourceEvent myEvent = (MessageSourceEvent) event;<?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans><?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.         <bean id="userService" />
  66. </beans>myEvent.print();<?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.         <bean id="userService" />
  71. </beans><?xml version="1.0" encoding="UTF-8"?>
  72. <beans xmlns="http://www.springframework.org/schema/beans"
  73.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  75.         <bean id="userService" />
  76. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.         <bean id="userService" />
  81. </beans>}}
复制代码
监听二
  1. //在Spring 中使用事件监听机制(事件、监听、发布)//监听类,在spring配置文件中,注册事件类和监听类public class ApplicationContextListenerTwo implements ApplicationListener  {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>@Override<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans>public void onApplicationEvent(ApplicationEvent event) {<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans>if(event instanceof MessageSourceEvent){<?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans><?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans><?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans>System.out.println("进入到监听器类---two");<?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans><?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans><?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans>MessageSourceEvent myEvent=(MessageSourceEvent)event;<?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans><?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.         <bean id="userService" />
  66. </beans>myEvent.print();<?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.         <bean id="userService" />
  71. </beans><?xml version="1.0" encoding="UTF-8"?>
  72. <beans xmlns="http://www.springframework.org/schema/beans"
  73.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  75.         <bean id="userService" />
  76. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.         <bean id="userService" />
  81. </beans>}}
复制代码
发布事件
  1. //在Spring 中使用事件监听机制(事件、监听、发布)//该类实现ApplicationContextAware接口,得到ApplicationContext对象// 使用该对象的publishEvent方法发布事件public class ApplicationContextListenerPubisher implements ApplicationContextAware {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>private ApplicationContext applicationContext;<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans>@Override<?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {<?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans><?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans>this.applicationContext = applicationContext;<?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans>public void publishEvent(ApplicationEvent event) {<?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans><?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans>System.out.println("发布事件");<?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans><?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans>applicationContext.publishEvent(event);<?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans>}}
复制代码
配置文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans><?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans><?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans><?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans><?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans><?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans>
复制代码
测试
  1. //总结 :使用bean工厂发布和使用多播器效果是一样的public class Test {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.         <bean id="userService" />
  6. </beans>public static void main(String[] args) {<?xml version="1.0" encoding="UTF-8"?>
  7. <beans xmlns="http://www.springframework.org/schema/beans"
  8.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  9.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  10.         <bean id="userService" />
  11. </beans><?xml version="1.0" encoding="UTF-8"?>
  12. <beans xmlns="http://www.springframework.org/schema/beans"
  13.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  15.         <bean id="userService" />
  16. </beans>ApplicationContext context =<?xml version="1.0" encoding="UTF-8"?>
  17. <beans xmlns="http://www.springframework.org/schema/beans"
  18.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  19.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  20.         <bean id="userService" />
  21. </beans><?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  25.         <bean id="userService" />
  26. </beans><?xml version="1.0" encoding="UTF-8"?>
  27. <beans xmlns="http://www.springframework.org/schema/beans"
  28.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  29.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  30.         <bean id="userService" />
  31. </beans><?xml version="1.0" encoding="UTF-8"?>
  32. <beans xmlns="http://www.springframework.org/schema/beans"
  33.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  34.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  35.         <bean id="userService" />
  36. </beans>new ClassPathXmlApplicationContext("classpath*:application.xml");<?xml version="1.0" encoding="UTF-8"?>
  37. <beans xmlns="http://www.springframework.org/schema/beans"
  38.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  40.         <bean id="userService" />
  41. </beans><?xml version="1.0" encoding="UTF-8"?>
  42. <beans xmlns="http://www.springframework.org/schema/beans"
  43.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  44.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  45.         <bean id="userService" />
  46. </beans>//***************使用spring的多播器发布**********************<?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.         <bean id="userService" />
  51. </beans><?xml version="1.0" encoding="UTF-8"?>
  52. <beans xmlns="http://www.springframework.org/schema/beans"
  53.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  55.         <bean id="userService" />
  56. </beans>ApplicationEventMulticaster applicationEventMulticaster = (ApplicationEventMulticaster) context.getBean("applicationEventMulticaster");<?xml version="1.0" encoding="UTF-8"?>
  57. <beans xmlns="http://www.springframework.org/schema/beans"
  58.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  60.         <bean id="userService" />
  61. </beans><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  65.         <bean id="userService" />
  66. </beans>applicationEventMulticaster.multicastEvent(new MessageSourceEvent("测试..."));//***************使用BeanFactory的publishEvent发布*********************<?xml version="1.0" encoding="UTF-8"?>
  67. <beans xmlns="http://www.springframework.org/schema/beans"
  68.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  69.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  70.         <bean id="userService" />
  71. </beans><?xml version="1.0" encoding="UTF-8"?>
  72. <beans xmlns="http://www.springframework.org/schema/beans"
  73.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  74.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  75.         <bean id="userService" />
  76. </beans>//<?xml version="1.0" encoding="UTF-8"?>
  77. <beans xmlns="http://www.springframework.org/schema/beans"
  78.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  80.         <bean id="userService" />
  81. </beans>ApplicationContextListenerPubisher myPubisher = (ApplicationContextListenerPubisher)<?xml version="1.0" encoding="UTF-8"?>
  82. <beans xmlns="http://www.springframework.org/schema/beans"
  83.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  84.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  85.         <bean id="userService" />
  86. </beans><?xml version="1.0" encoding="UTF-8"?>
  87. <beans xmlns="http://www.springframework.org/schema/beans"
  88.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  89.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  90.         <bean id="userService" />
  91. </beans>//context.getBean("applicationContextListenerPubisher");<?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  95.         <bean id="userService" />
  96. </beans><?xml version="1.0" encoding="UTF-8"?>
  97. <beans xmlns="http://www.springframework.org/schema/beans"
  98.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  99.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  100.         <bean id="userService" />
  101. </beans>//myPubisher.publishEvent(new MessageSourceEvent("测试..."));<?xml version="1.0" encoding="UTF-8"?>
  102. <beans xmlns="http://www.springframework.org/schema/beans"
  103.            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  104.            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  105.         <bean id="userService" />
  106. </beans>}}
复制代码
多播发布

工厂发布

总结:
​       1、spring的事件驱动模型使用的是 观察者模式
  2、通过ApplicationEvent抽象类和ApplicationListener接口,可以实现事件处理
  3、ApplicationEventMulticaster事件广播器实现了监听器的注册,一般不需要我们实现,只需要显示的调用    applicationcontext.publisherEvent方法即可
​        4、使用bean工厂发布和使用多播器效果是一样的
本文由传智教育博学谷教研团队发布。
如果本文对您有帮助,欢迎关注和点赞;如果您有任何建议也可留言评论或私信,您的支持是我坚持创作的动力。
转载请注明出处!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

写过一篇

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表