一、Spring 核心概念

  • IoC(核心中的核心):Inverse of Control, 控制反转,对象创建的权利由程序交给Spring框架
  • DI(依赖注入):Dependency Injection, 依赖注入。在Spring框架负责创建对象时,动态的将依赖对象注入到Bean组件中
  • AOP:面向切面编程。在不修改目标对象源码的情况下,增强IoC容器中的功能
  • Spring容器:指的就是IoC 容器,底层也就是一个类DefaultListableBeanFactory

Spring 概览图
Spring概述.png

二、Spring 体系结构

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12747730/1637906499789-6d36c014-51ef-458d-8514-e28c511d53ba.png#clientId=uba4477c3-a796-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=231&id=ub24686ce&margin=%5Bobject%20Object%5D&name=image.png&originHeight=416&originWidth=540&originalType=binary&ratio=1&rotation=0&showTitle=false&size=51329&status=done&style=none&taskId=u96b1661f-dc0d-4f5c-9732-453a1978345&title=&width=300) ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12747730/1640657958866-e6c24e3d-3418-470b-9608-4f30f5fc5a68.png#clientId=u74cee23b-ad3d-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=290&id=u2ce05a3a&margin=%5Bobject%20Object%5D&name=image.png&originHeight=579&originWidth=463&originalType=binary&ratio=1&rotation=0&showTitle=false&size=77060&status=done&style=none&taskId=ucfab179a-964b-4fcc-bee1-de512514927&title=&width=231.5)

从上面Spring的体系结构中可以看到,Spring主要分为5大模块,一些组件都是整合在大的模块中。接下来简单介绍一下5大核心模块的功能和依赖关系。

2.1 核心容器(Core Container)

基本介绍:

Core和 Beans 模块提供了Spring最基础的功能,提供了IoC和依赖注入特性。这里的核心概念BeanFactory,它提供对Factory模式的经典实现来消除对程序性单例模式的需要,并真正地允许你从程序逻辑中分离出依赖关系和配置。
Context模块基于Core和Bean来构建,它提供了用一种框架风格地方式来访问对象,有些像JNDI注册表。Context封装包继承了beans包的功能,还增加了国际化(I18N),事件传播,资源装载,以及透明创建上下文,例如通过servlet容器,以及对大量JavaEE特性的支持,如EJB,JMX.核心接口是ApplicationContext.
Expression Language,表达式语言模块,提供了在运行期间查询和操作对象图的强大能力。支持访问和修改属性值,方法调用,支持访问及修改数组、容器和索引器,命名变量,支持算数和逻辑运算,支持从spring容器获取Bean,它也支持列表投影、选择和一般的列表聚合等。

core:
core部分包含4个模块

  1. spring-core:依赖注入DI与IoC的最基本实现
  2. spring-beans:Bean工厂与bean的装配
  3. spring-context:spring的context上下文即IoC容器
  4. spring-expression:spring表达式语言

四个模块之间的依赖关系图:
image.png

2.2 AOP

基本概述:

aop模块,提供了符合aop联盟规范的面向切面的编程实现,让你可以定义如方法拦截器和切入点,从逻辑上讲,可以减弱代码的功能耦合,清晰地被分离开。而且,利用源码级地元数据功能,还可以将各种行为信息合并到你的代码中。 aspects模块,提供了对AspectJ的集成。 Instrumentation模块,提供一些类级的工具支持和ClassLoader级的实现,可以在一些特定的应用服务器中使用。

aop
aop部分包含4个模块

  1. spring-aop:面向切面编程
  2. spring-aspects:集成AspectJ
  3. spring-instrument:提供一些类级的工具支持和ClassLoader级的实现,用于服务器
  4. spring-instrument-tomcat:针对tomcat的instrument实现

它们的依赖关系
image.png

2.3 数据访问/集成部分(Data Access/Integration)

基本概述:

JDBC模块:提供对JDBC的抽象,它可消除冗长的JDBC编码和解析数据库厂商特有的错误代码 ORM模块:提供了常用的“对象/关系”映射API的集成层。其中包括JPA、JDO、Hibernate和iBatis.利用ORM封装包,可以混合使用所有Spring提供的特性进行“对象/关系”映射,如简单声明式事务管理。 OXM模块:提供一个支持Object和XML进行映射的抽象层。其中包括JAXB、Castor、XMLBeans、JiBX和XStream. JMS模块:提供一套“消息生产者、消费者”模板用于更加简单的使用JMS,JMS用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。 Transaction模块:支持程序通过简单声明式事务管理,只要是Spring管理对象都能得到Spring管理事务的好处,即使是POJO,也可以为他们提供事务。

data access
data access部分包含5个模块

  1. spring-jdbc:jdbc的支持
  2. spring-tx:事务控制
  3. spring-orm:对象关系映射,集成orm框架
  4. spring-oxm:对象xml映射
  5. spring-jms:java消息服务

它们的依赖关系
image.png

二、Spring 应用

常用注解

Spring中常用的注解主要分为两类: 一、使用Bean:即是把在XML中已经定义好的Bean拿来使用,完成属性、方法的组装,比如@Autowired,@Resource,可以通过byTYPE(@Autowired)、byNAME(@Resource)的方式获取Bean; 二、注册Bean: @Component , @Repository , @ Controller , @Service , @Configration这些注解都是把你要实例化的对象转化成一个Bean,放在IoC容器中,等你要用的时候,它会和上面的@Autowired@Resource配合到一起,把对象、属性、方法完美组装。

IOC注解(创建对象)

@Component

  • 作用:

    1. 把资源交给Spring 来管理,相当于XML中的一个Bean 标签
  • 属性:

    1. value:
    2. 指定beanid, 如果不指定value 属性,默认bean id 是当前类名,首字母小写

    @Controller、@Service、@Repository

    他们三个注解都是对针对@Component的衍生注解,他们的作用及属性都是一模一样的,他们只不过是定义了更加明确化的语义。

    DI注解(依赖注入)

    @Autowired

  • @Autowired默认按照类型装配(byType)

  • @Autowired是由AutowiredAnnotationBeanPostProcessor 类实现的
  • @Autowired 是Spring自带的注解
  • @Autowired 默认情况下要求对象必须存在,如果需要允许空值,可以设置他的required属于为false,@Autowired(required = false)

    �@Resource

  • resource 默认按照名称进行装配(byName),可以通过@Resource的name属性指定名称,如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查询,当找不到与名称匹配的bean后按照类型进行装配

  • 但是需要注意的是,name属性一旦指定,就将按照名称装配,不会按照类型进行装配
  • 推荐使用这个注解,因为它属于J2EE的,减少了与Spring的耦合,这样代码看起来比较优雅。

    @Inject

  • 是根据类型进行装配的,如果需要按照名称进行装配就需要和@Named配合使用

  • inject 是JSR330 中的规范,需要导入javax.inject.Inject; 实现注入
  • Inject 可以用在变量、setter方法、构造函数

    @Value

  • 给基本类型和String注入属性值

  • 可以使用占位符获取属性文件中的值
    1. @Value("${name}")
    2. private String name;

    @Qualifier

@Autowired、@Inject、@Resource 三者区别

  1. Autowired 是Spring的注解,Inject和Resource 非Sping注解需要导入其他包
  2. Autowired 和 Inject 用法基本一样,区别的是 Autowired 多一个属性 required
  3. Autowired 和 Inject 都是按照类型进行装配,Resource按照名称进行装配
  4. Autowired 如果需要按照名称装配需要和Qualifier配合使用,Inject 需要和Named 配合使用

    生命周期相关注解

    @PostConstruct
    @PreDestory

    系统配置注解

    @Configuration

    1. 相当于Spring 中的XML配置文件
    2. Spring3.0开始可以使用Configuration定义配置类,可替换xml配置文件
    3. 配置类内部包含一个或多个被@Bean注解定义的方法,这些类被AnnotationConfigApplicationContext
    4. AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean 对象,初始化IOC容器
  • 属性

    value:用于指定配置类的字节码文件

  • 代码示例

Spring容器初始化时,会调用配置类的无参构造

  1. @Configuration
  2. @MapperScan(basePackages = {"com.bj58.lego.ssp.modules.base.dao.mapper",
  3. "com.bj58.lego.ssp.modules.flowmanage.dao.mapper",
  4. "com.bj58.lego.ssp.modules.product.mapper",
  5. "com.bj58.lego.ssp.modules.sale.mapper",
  6. "com.bj58.lego.ssp.modules.discount.mapper"}, sqlSessionTemplateRef = "primarySqlSessionTemplate")
  7. public class PrimaryMpConfig {
  8. @Bean(name = "primarySqlSessionFactory")
  9. @Primary
  10. public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
  11. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  12. MybatisConfiguration configuration = new MybatisConfiguration();
  13. configuration.addInterceptor(new PaginationInterceptor());
  14. bean.setConfiguration(configuration);
  15. bean.setDataSource(dataSource);
  16. //bean.setPlugins(new Interceptor[]{new PaginationInterceptor()});
  17. ArrayList<String> resourceStrs = new ArrayList<>();
  18. resourceStrs.add("classpath*:mapper/base/*.xml");
  19. resourceStrs.add("classpath*:mapper/sale/*.xml");
  20. ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
  21. ArrayList<Resource> resources = new ArrayList<>();
  22. for (String resourceStr : resourceStrs) {
  23. resources.addAll(Arrays.asList(resourceResolver.getResources(resourceStr)));
  24. }
  25. bean.setMapperLocations(resources.toArray(new Resource[0]));
  26. return bean.getObject();
  27. }
  28. @Bean(name = "primarySqlSessionTemplate")
  29. @Primary
  30. public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
  31. return new SqlSessionTemplate(sqlSessionFactory);
  32. }
  33. }

@Bean

  • 相当于 bean 标签
  • 作用:注册bean 对象,主要用来配置非定义的bean,比如DruidDataSource
  • @Bean 用在方法上(返回某个实例的方法)

属性:

  1. name:给当前@Bean注解方法创建的对象指定一个名称(即Beanid),如果不指定,默认和方法名相同

@ComponentScan

  • 相当于context:component-sacn 标签
  • 组件扫描,扫描 @Service、@Compinent、@Controller、@Repository 注解的类
  • 该注解是编写在类上面的,配合@Configutation注解一起使用

属性:

  1. basePackages: 用于指定包扫描
  2. value: basePackages 作用一样

代码:

  1. @Configuration
  2. @ComponentScan(basePackages = "com.bj58.lego.bi.modules.*")
  3. public class FourthMpConfig {
  4. @Bean(name = "fourthSqlSessionFactory")
  5. @Primary
  6. public SqlSessionFactory sqlSessionFactory (@Qualifier("fourthDataSource") DataSource dataSource) throws Exception {
  7. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  8. bean.setDataSource(dataSource);
  9. bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/BiRealDataMapper.xml"));
  10. return bean.getObject();
  11. }
  12. @Bean(name = "fourthSqlSessionTemplate")
  13. @Primary
  14. public SqlSessionTemplate primarySqlSessionTemplate (@Qualifier("fourthSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
  15. return new SqlSessionTemplate(sqlSessionFactory);
  16. }
  17. }

@PropertySource

  • 用在类上面,主要用于加载Properties 配置文件

属性:
value[]:用于指定properties 文件的位置,如果在类路径下,需要写上calssPath

@Import

三、Spring 重要接口

2.1 BeanFactroy继承体系

IOC和DI: 控制反转、依赖注入 IOC容器:Spring容器(Bean)———BeanFactory接口———DefaultListableBeanFactry实现类———Map数据结构存储bean

2.1.1 继承关系图

这是banFactory的整个继承体系结构图,没有包括强大的ApplicationContext体系。标红的是整个体系结构外接的其他体系结构。
image.png
beanFactory 四级接口继承体系:

  1. beanFactory 作为一个主接口不继承任何接口,可以看作为一级接口
  2. AutowireCapableBeanFactory、ListableBeanFactory、HierarchicalBeanFactory 3个子接口继承了beanFactory, 进行了功能上的增强。这三个子接口称为二级接口
  3. ConfigurableBeanFactory 可以称为三级接口,对二级接口HierarchicalBeanFactroy进行了再次增强,它还继承了另外一个外来的接口SignletonBeanRegistry
  4. ConfigurableListBeanFactory 是一个更强大的接口,它继承了所有上面的接口,无所不包,称为四级接口

总结:

|— BeanFactory: 是Sping 容器的根接口,提供获取bean、是否包含bean、是否单例与原型、获取bean类型的接口 |— — AutowireCapableBeanFactory: 提供工厂的装备功能 |— — ListableBeanFactory: 提供容器内所有bean实例的枚举功能 |— — HierarchicalBeanFactory:提供父容器的访问功能 |— — — ConfigurableBeanFactory: 提供beanFactory的装备功能

重要2个接口和2个抽象类:

  1. AbstractBeanFactory作为一个抽象类,实现了三级接口ConfigurableBeanFactory的大部分功能
  2. AbstractAutowireCapableBeanFactory 同样是抽象类,继承了AbstractBeanFactory类,实现了

AutowireCapableBeanFactory接口

  1. DefaultListableBeanFactory 继承自AbstractAutowireCapableBeanFactory,实现了强大的四级接口ConfigurableListBeanFactory,并实现了一个外来接口BeanDifinitionRegistry,它并非抽象类。

    BeanFactory

    1. package org.springframework.beans.factory;
    2. public interface BeanFactory {
    3. //用来引用一个实例,或把它和工厂产生的Bean区分开
    4. //就是说,如果一个FactoryBean的名字为a,那么,&a会得到那个Factory
    5. String FACTORY_BEAN_PREFIX = "&";
    6. /** 四个不同形式的getBean方法,获取实例 */
    7. Object getBean(String name) throws BeansException;
    8. <T> T getBean(String name, Class<T> requiredType) throws BeansException;
    9. <T> T getBean(Class<T> requiredType) throws BeansException;
    10. Object getBean(String name, Object... args) throws BeansException;
    11. // 是否存在
    12. boolean containsBean(String name);
    13. // 是否为单实例
    14. boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
    15. // 是否为原型(多实例)
    16. boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
    17. // 名称、类型是否匹配
    18. boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;
    19. // 获取类型
    20. Class<?> getType(String name) throws NoSuchBeanDefinitionException;
    21. // 根据实例的名字获取实例的别名
    22. String[] getAliases(String name);
    23. }

    2.2 ApplicationContext继承体系

    2.2.1 类继承关系图

    ApplicationContext 允许上下文嵌套,通过保持父上下文可以维持一个上下文体系。对于Bean的查找可以在这个上下文体系中发生,首先是检查上下文,其次是父上下文,逐级向上,这样为不同的Spring 应用提供了一个共享的Bean定义环境。
    image.png
    图片.png

四、IOC 容器

一、Spring IOC 容器基本原理

  1. 两个核心容器—-map

Map beanDefinitionMap; 这个容器位于 DefaultListableBeanFactory类中

  1. /** Map of bean definition objects, keyed by bean name */
  2. private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);

Map singletonObjects; 这个容器位于 DefaultSingletonBeanRegistry 类中

  1. public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {
  2. /** Cache of singleton objects: bean name --> bean instance */
  3. /** 一级缓存 **/
  4. private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);

二、Spring 容器初始化过程

2.1 主流程源码分析

2.2.1 流程入口

  1. ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

2.2.2 流程分析

  • AbstractApplicationContext的 refresh()方法

    1. @Override
    2. public void refresh() throws BeansException, IllegalStateException {
    3. synchronized (this.startupShutdownMonitor) {
    4. // Prepare this context for refreshing.
    5. // STEP 1: 刷新预处理
    6. /**
    7. * 1.
    8. * 2.
    9. * 3.
    10. * 4.
    11. */
    12. prepareRefresh();
    13. // Tell the subclass to refresh the internal bean factory.
    14. // STEP 2: 创建并获得beanFactory
    15. /**
    16. * 1. AbstractRefreshableApplicationContext.refreshBeanFactory(); 预刷新 beanFactory
    17. * a) 创建IoC容器(DefaultListableBeanFactory)
    18. * b) 加载解析XML文件(最终存储到Document对象中)
    19. * c) 读取Document对象,并完成BeanDefinition的加载和注册工作
    20. */
    21. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    22. // Prepare the bean factory for use in this context.
    23. // STEP 3:对IoC容器进行一些预处理(设置一些公共属性)
    24. prepareBeanFactory(beanFactory);
    25. try {
    26. // Allows post-processing of the bean factory in context subclasses.
    27. //STEP 4: 设置beanFactory 的一些属性
    28. postProcessBeanFactory(beanFactory);
    29. // Invoke factory processors registered as beans in the context.
    30. //STEP 5: 调用BeanFactoryPostProcessor后置处理器对BeanDefinition处理
    31. invokeBeanFactoryPostProcessors(beanFactory);
    32. // Register bean processors that intercept bean creation.
    33. //STEP 6: 注册BeanPostProcessor后置处理器
    34. registerBeanPostProcessors(beanFactory);
    35. // Initialize message source for this context.
    36. //STEP 7: 初始化一些消息源(比如处理国际化的i18n等消息源)
    37. initMessageSource();
    38. // Initialize event multicaster for this context.
    39. //STEP 8: 初始化应用事件广播器
    40. initApplicationEventMulticaster();
    41. // Initialize other special beans in specific context subclasses.
    42. //STEP 9: 初始化一些特殊的bean
    43. onRefresh();
    44. // Check for listener beans and register them.
    45. // STEP 10: 注册一些监听器
    46. registerListeners();
    47. // Instantiate all remaining (non-lazy-init) singletons.
    48. // STEP 11: 实例化剩余的单例bean(非懒加载方式)
    49. // 注意事项:Bean的IoC、DI和AOP都是发生在此步骤
    50. finishBeanFactoryInitialization(beanFactory);
    51. // Last step: publish corresponding event.
    52. // 完成Spring整个对象的创建,生成一个完整的对象
    53. finishRefresh();
    54. }
    55. catch (BeansException ex) {
    56. if (logger.isWarnEnabled()) {
    57. logger.warn("Exception encountered during context initialization - " +
    58. "cancelling refresh attempt: " + ex);
    59. }
    60. // Destroy already created singletons to avoid dangling resources.
    61. destroyBeans();
    62. // Reset 'active' flag.
    63. cancelRefresh(ex);
    64. // Propagate exception to caller.
    65. throw ex;
    66. }
    67. finally {
    68. // Reset common introspection caches in Spring's core, since we
    69. // might not ever need metadata for singleton beans anymore...
    70. resetCommonCaches();
    71. }
    72. }
    73. }

    2.2 创建BeanFactroy流程分析

    2.2.1 入口

  • AbstractApplicationContext类的 refresh 方法: ```java /**

    1. AbstractRefreshableApplicationContext.refreshBeanFactory(); 预刷新 beanFactory
  • a) 创建IoC容器(DefaultListableBeanFactory)
  • b) 加载解析XML文件(最终存储到Document对象中)
  • c) 读取Document对象,并完成BeanDefinition的加载和注册工作 */ ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); ```

    2.2.2 流程源码分析

  • 进入AbstractBeanFactory的obtainFreshBeanFactoy()方法中:

    用于创建一个新的 IoC容器 ,这个 IoC容器 就是DefaultListableBeanFactory对象。

    1. /**
    2. * Tell the subclass to refresh the internal bean factory.
    3. * @return the fresh BeanFactory instance
    4. * @see #refreshBeanFactory()
    5. * @see #getBeanFactory()
    6. */
    7. protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
    8. // 主要通过该方法创建IOC容器
    9. refreshBeanFactory();
    10. ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    11. if (logger.isDebugEnabled()) {
    12. logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
    13. }
    14. return beanFactory;
    15. }
  • 进入AbstractRefreshableApplicationContext的 refreshBeanFactory 方法:

    • 销毁以前的容器
    • 创建新的容器
    • 加载BeanDifinition对象到IOC容器中 ```java @Override protected final void refreshBeanFactory() throws BeansException { // 销毁以前的IOC容器 if (hasBeanFactory()) {
      1. destroyBeans();
      2. closeBeanFactory();
      } try {
      1. // 创建新的 IOC 容器
      2. DefaultListableBeanFactory beanFactory = createBeanFactory();
      3. // 为每个容器设置一个序列化ID
      4. beanFactory.setSerializationId(getId());
      5. // 增强定制 beanFactory
      6. customizeBeanFactory(beanFactory);
      7. // 加载BeanDefinition 对象, 并注册到IOC容器中(重点)
      8. loadBeanDefinitions(beanFactory);
      9. synchronized (this.beanFactoryMonitor) {
      10. this.beanFactory = beanFactory;
      11. }
      } catch (IOException ex) {
      1. throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
      } }
  1. - **进入到AbstractRefreshableApplicationContextcreateBeanFactory**
  2. ```java
  3. // 返回 DefaultListableBeanFactory 对象, 真正的IOC容器
  4. protected DefaultListableBeanFactory createBeanFactory() {
  5. return new DefaultListableBeanFactory(getInternalParentBeanFactory());
  6. }

2.4 加载BeanDifinition流程分析

2.4.1 入口

  • AbstractRefreshableApplicationContext类的 refreshBeanFactory 方法中第13行代码 loadBeanDefinition()
    1. @Override
    2. protected final void refreshBeanFactory() throws BeansException {
    3. // 销毁以前的IOC容器
    4. if (hasBeanFactory()) {
    5. destroyBeans();
    6. closeBeanFactory();
    7. }
    8. try {
    9. // 创建新的 IOC 容器
    10. DefaultListableBeanFactory beanFactory = createBeanFactory();
    11. // 为每个容器设置一个序列化ID
    12. beanFactory.setSerializationId(getId());
    13. // 增强定制 beanFactory
    14. customizeBeanFactory(beanFactory);
    15. // 加载BeanDefinition 对象, 并注册到IOC容器中(重点)
    16. // 加载 beanDefinition 的入口
    17. loadBeanDefinitions(beanFactory);
    18. synchronized (this.beanFactoryMonitor) {
    19. this.beanFactory = beanFactory;
    20. }
    21. }
    22. catch (IOException ex) {
    23. throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
    24. }
    25. }

    2.4.2 流程图

    loadBeanDefinitions() 时序图:
    image.png
    loadBeanDefinitions() 方法变化过程图:
    image.png

2.4.3 流程相关的类说明

  • AbstractRefreshableApplicationContext

主要用来为beanFactory 提供 refresh功能。主要包括:beanFactory的创建、beanDefinition定义、解析、注册等。

  • AbstractXmlApplicationContext

主要提供对于 XML资源 的加载功能。包括从Resource资源对象和资源路径中加载XML文件。

  • AbstractBeanDefinitionReader

    主要提供对于 BeanDefinition 对象的读取功能。具体读取工作交给子类实现。

  • XmlBeanDefinitionReader

    主要通过 DOM4J 对于 XML资源 的读取、解析功能,并提供对于 BeanDefinition 的注册功能。

  • DefaultBeanDefinitionDocumentReader

  • BeanDefinitionParserDelegate

    2.4.4 流程源码分析

  • AbstractRefreshableApplicationContext 类的 138 进行调用loadBeanDefinitions(beanFactory)

    1. @Override
    2. protected final void refreshBeanFactory() throws BeansException {
    3. // 销毁以前的IOC容器
    4. if (hasBeanFactory()) {
    5. destroyBeans();
    6. closeBeanFactory();
    7. }
    8. try {
    9. // 创建新的 IOC 容器
    10. DefaultListableBeanFactory beanFactory = createBeanFactory();
    11. // 为每个容器设置一个序列化ID
    12. beanFactory.setSerializationId(getId());
    13. // 增强定制 beanFactory
    14. customizeBeanFactory(beanFactory);
    15. // 加载BeanDefinition 对象, 并注册到IOC容器中(重点)
    16. loadBeanDefinitions(beanFactory);
    17. synchronized (this.beanFactoryMonitor) {
    18. this.beanFactory = beanFactory;
    19. }
    20. }
    21. catch (IOException ex) {
    22. throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
    23. }
    24. }
  • AbstractXmlApplicationContext 类的 loadBeanDefinitions(BeanFactory)

    • 创建一个xml的beanDefinitionReader,并通过回调设置到beanFactory中
    • 给reader 对象设置环境
    • 初始化 beanDifinitionReader 对象,设置配置文件是否要进行验证
    • 开始完成beanDifiniton的加载

      1. @Override
      2. protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
      3. // Create a new XmlBeanDefinitionReader for the given BeanFactory.
      4. // 1.创建一个xml的beanDefinitionReader,并通过回调设置到beanFactory中
      5. XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
      6. // Configure the bean definition reader with this context's
      7. // resource loading environment.
      8. //2. 给reader 对象设置环境
      9. beanDefinitionReader.setEnvironment(this.getEnvironment());
      10. beanDefinitionReader.setResourceLoader(this);
      11. beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
      12. // Allow a subclass to provide custom initialization of the reader,
      13. // then proceed with actually loading the bean definitions.
      14. // 3. 初始化 beanDifinitionReader 对象,设置配置文件是否要进行验证
      15. initBeanDefinitionReader(beanDefinitionReader);
      16. // 4. 开始完成beanDifiniton的加载
      17. loadBeanDefinitions(beanDefinitionReader);
      18. }
  • AbstractXmlApplicationContext 类的 loadBeanDefinitions(XmlBeanDefinitionReader)

    • 获取资源配置,默认为空
    • 获取资源路径,得到String[], 注意此处的 getConfigLocations(),在refresh()流程中有过setConfigLocation() 方法,其实两者是相互对应的
      1. protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
      2. Resource[] configResources = getConfigResources();
      3. if (configResources != null) {
      4. reader.loadBeanDefinitions(configResources);
      5. }
      6. String[] configLocations = getConfigLocations();
      7. if (configLocations != null) {
      8. reader.loadBeanDefinitions(configLocations);
      9. }
      10. }
  • AbstractBeanDefinitionReader 类的 loadBeanDefinitions(String… locations)

    • for循环处理得到的 String[] 资源位置 ```java @Override public int loadBeanDefinitions(String… locations) throws BeanDefinitionStoreException { Assert.notNull(locations, “Location array must not be null”); int counter = 0; for (String location : locations) {
      1. counter += loadBeanDefinitions(location);
      } return counter; }
  1. - **AbstractBeanDefinitionReader 类的 loadBeanDefinitions(String location)**
  2. - **根据 location 获取得到资源数据 Resource[] /Resource**
  3. - **解析Resource 实体进行BeanDefinition的加载**
  4. ```java
  5. // Load bean definitions from the specified resource location.
  6. public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {
  7. // 获取资源加载器
  8. ResourceLoader resourceLoader = getResourceLoader();
  9. if (resourceLoader == null) {
  10. throw new BeanDefinitionStoreException(
  11. "Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
  12. }
  13. if (resourceLoader instanceof ResourcePatternResolver) {
  14. // Resource pattern matching available.
  15. try {
  16. // 通过location 获取得到资源 Resource[]
  17. Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
  18. // 解析resource 然后加载BeanDefinition
  19. int loadCount = loadBeanDefinitions(resources);
  20. if (actualResources != null) {
  21. for (Resource resource : resources) {
  22. actualResources.add(resource);
  23. }
  24. }
  25. if (logger.isDebugEnabled()) {
  26. logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
  27. }
  28. return loadCount;
  29. }
  30. catch (IOException ex) {
  31. throw new BeanDefinitionStoreException(
  32. "Could not resolve bean definition resource pattern [" + location + "]", ex);
  33. }
  34. }
  35. else {
  36. // Can only load single resources by absolute URL.
  37. // 通过location 获取得到资源 Resource
  38. Resource resource = resourceLoader.getResource(location);
  39. // 解析resource 然后加载BeanDefinition
  40. int loadCount = loadBeanDefinitions(resource);
  41. if (actualResources != null) {
  42. actualResources.add(resource);
  43. }
  44. if (logger.isDebugEnabled()) {
  45. logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
  46. }
  47. return loadCount;
  48. }
  49. }
  • BeanDefinitionReader 类的 loadBeanDefinitions(Resource resource)
    • 该类是一个抽象类,由具体的实现类进行解析读取到的 resource
  • XmlBeanDefinitionReader 类的 loadBeanDefinitions(EncodedResource encodedResource)

    • 真正的解析读取资源,得到输入流 InputStream (所谓的资源一般是指xml 等配置文件)
    • 执行最核心的加载beanDefinition方法 doLoadBeanDefinitions() ```java // Load bean definitions from the specified XML file. public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException { Assert.notNull(encodedResource, “EncodedResource must not be null”); if (logger.isInfoEnabled()) {

      1. logger.info("Loading XML bean definitions from " + encodedResource);

      }

      Set currentResources = this.resourcesCurrentlyBeingLoaded.get(); if (currentResources == null) {

      1. currentResources = new HashSet<>(4);
      2. this.resourcesCurrentlyBeingLoaded.set(currentResources);

      } if (!currentResources.add(encodedResource)) {

      1. throw new BeanDefinitionStoreException(
      2. "Detected cyclic loading of " + encodedResource + " - check your import definitions!");

      } try {

      1. InputStream inputStream = encodedResource.getResource().getInputStream();
      2. try {
      3. // STEP 1: 解析读取XML文件得到输入流
      4. InputSource inputSource = new InputSource(inputStream);
      5. if (encodedResource.getEncoding() != null) {
      6. inputSource.setEncoding(encodedResource.getEncoding());
      7. }
      8. // STEP 2:执行最核心的加载beanDefinition方法
      9. return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
      10. }
      11. finally {
      12. inputStream.close();
      13. }

      } catch (IOException ex) {

      1. throw new BeanDefinitionStoreException(
      2. "IOException parsing XML document from " + encodedResource.getResource(), ex);

      } finally {

      1. currentResources.remove(encodedResource);
      2. if (currentResources.isEmpty()) {
      3. this.resourcesCurrentlyBeingLoaded.remove();
      4. }

      } }

  1. - **XmlBeanDefinitionReader 类中的 doLoadBeanDefinition()方法**
  2. - **读取资源输入流生成java 程序可以解析的 Document**
  3. - **解析Document 进行beanDefiniiton的真正注册流程**
  4. ```java
  5. protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
  6. throws BeanDefinitionStoreException {
  7. try {
  8. // 根据资源输入流创建 Document,
  9. Document doc = doLoadDocument(inputSource, resource);
  10. // 解析Java 程序可以解析的 doc 文档注册beanDefinition
  11. return registerBeanDefinitions(doc, resource);
  12. }
  13. catch (BeanDefinitionStoreException ex) {
  14. throw ex;
  15. }
  16. ......
  17. }
  18. }
  • XmlBeanDefinitionReade 类中的 registerBeanDefinitions(doc, resource)

    • 通过beanUtils 创建 BeanDefinitionDocumentReade 对象
    • 调用获取 DefaultListbaleBeanFactory 中的 beanDefinitionMap 大小
    • 解析Document 进行 BeanDefiniiton 注册
      1. public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
      2. // 使用 BeanUtils 创建 BeanDefinitionDocumentReader 对象
      3. BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
      4. // 获取 得到 BeanDefinitionRegistry,然后获取 DefaultListableBeanFactory 中 beanDefinitionMap 大小
      5. int countBefore = getRegistry().getBeanDefinitionCount();
      6. // 解析document文档 然后注册到容器中
      7. documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
      8. return getRegistry().getBeanDefinitionCount() - countBefore;
      9. }
  • DefaultBeanDefinitionDoucntmentReader 类的 registerBeanDefinitions() ```java @Override public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {

    1. this.readerContext = readerContext;
    2. logger.debug("Loading bean definitions");
    3. Element root = doc.getDocumentElement();
    4. doRegisterBeanDefinitions(root);

    } /**

    • Register each bean definition within the given root {@code } element. / //任何嵌套的元素都将在此方法中导致递归。为了正确传播和保留 default-属性,请跟踪当前(父)委托,该委托可能为空。 //创建一个新的(子)委托,其中包含一个对父委托的引用,用于回退的目的,然后最终将this.delegate重置回其原始(父)引用。 //这种行为模拟了一堆委托,但实际上并不需要委托。 protected void doRegisterBeanDefinitions(Element root) {

      1. // Any nested <beans> elements will cause recursion in this method. In
      2. // order to propagate and preserve <beans> default-* attributes correctly,
      3. // keep track of the current (parent) delegate, which may be null. Create
      4. // the new (child) delegate with a reference to the parent for fallback purposes,
      5. // then ultimately reset this.delegate back to its original (parent) reference.
      6. // this behavior emulates a stack of delegates without actually necessitating one.
      7. BeanDefinitionParserDelegate parent = this.delegate;
      8. this.delegate = createDelegate(getReaderContext(), root, parent);
      9. if (this.delegate.isDefaultNamespace(root)) {
      10. String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
      11. if (StringUtils.hasText(profileSpec)) {
      12. String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
      13. profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
      14. if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
      15. if (logger.isInfoEnabled()) {
      16. logger.info("Skipped XML bean definition file due to specified profiles [" + profileSpec +
      17. "] not matching: " + getReaderContext().getResource());
      18. }
      19. return;
      20. }
      21. }
      22. }
      23. preProcessXml(root);
      24. parseBeanDefinitions(root, this.delegate);
      25. postProcessXml(root);
      26. this.delegate = parent;

      }

  1. - **DefaultBeanDefinitionDoucntmentReader 类的 doRegisterBeanDefinition()**
  2. ```java
  3. /**
  4. * Register each bean definition within the given root {@code <beans/>} element.
  5. */
  6. protected void doRegisterBeanDefinitions(Element root) {
  7. // Any nested <beans> elements will cause recursion in this method. In
  8. // order to propagate and preserve <beans> default-* attributes correctly,
  9. // keep track of the current (parent) delegate, which may be null. Create
  10. // the new (child) delegate with a reference to the parent for fallback purposes,
  11. // then ultimately reset this.delegate back to its original (parent) reference.
  12. // this behavior emulates a stack of delegates without actually necessitating one.
  13. BeanDefinitionParserDelegate parent = this.delegate;
  14. this.delegate = createDelegate(getReaderContext(), root, parent);
  15. if (this.delegate.isDefaultNamespace(root)) {
  16. String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
  17. if (StringUtils.hasText(profileSpec)) {
  18. String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
  19. profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
  20. if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
  21. if (logger.isInfoEnabled()) {
  22. logger.info("Skipped XML bean definition file due to specified profiles [" + profileSpec +
  23. "] not matching: " + getReaderContext().getResource());
  24. }
  25. return;
  26. }
  27. }
  28. }
  29. preProcessXml(root);
  30. parseBeanDefinitions(root, this.delegate);
  31. postProcessXml(root);
  32. this.delegate = parent;
  33. }
  • DefaultBeanDefinitionDoucntmentReader 类的 createDelegate() ```java protected BeanDefinitionParserDelegate createDelegate(

    1. XmlReaderContext readerContext, Element root, @Nullable BeanDefinitionParserDelegate parentDelegate) {
    2. BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
    3. delegate.initDefaults(root, parentDelegate);
    4. return delegate;

    }

  1. - ** BeanDefinitionParserDelegate类的 initDefaults()方法**
  2. ```java
  3. /**
  4. * Initialize the default lazy-init, autowire, dependency check settings,
  5. * init-method, destroy-method and merge settings. Support nested 'beans'
  6. * element use cases by falling back to the given parent in case the
  7. * defaults are not explicitly set locally.
  8. * @see #populateDefaults(DocumentDefaultsDefinition, DocumentDefaultsDefinition, org.w3c.dom.Element)
  9. * @see #getDefaults()
  10. */
  11. public void initDefaults(Element root, @Nullable BeanDefinitionParserDelegate parent) {
  12. populateDefaults(this.defaults, (parent != null ? parent.defaults : null), root);
  13. this.readerContext.fireDefaultsRegistered(this.defaults);
  14. }
  • BeanDefinitionParserDelegate 类的parseBeanDefinitions() 方法

    1. /**
    2. * Parse the elements at the root level in the document:
    3. * "import", "alias", "bean".
    4. * @param root the DOM root element of the document
    5. */
    6. protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
    7. if (delegate.isDefaultNamespace(root)) {
    8. NodeList nl = root.getChildNodes();
    9. for (int i = 0; i < nl.getLength(); i++) {
    10. Node node = nl.item(i);
    11. if (node instanceof Element) {
    12. Element ele = (Element) node;
    13. if (delegate.isDefaultNamespace(ele)) {
    14. parseDefaultElement(ele, delegate);
    15. }
    16. else {
    17. delegate.parseCustomElement(ele);
    18. }
    19. }
    20. }
    21. }
    22. else {
    23. delegate.parseCustomElement(root);
    24. }
    25. }

    2.5 Bean 实例化分析

    Spring 的核心模块,bean 的实例化和初始化两个过程。

    2.5.1 入口

  • AbstractApplicationContext类的 refresh 方法:

    1. // Instantiate all remaining (non-lazy-init) singletons.
    2. // STEP 11: 实例化剩余的单例bean(非懒加载方式)
    3. // 注意事项:Bean的IoC、DI和AOP都是发生在此步骤
    4. finishBeanFactoryInitialization(beanFactory);
  • AbstractApplicationContext类的 finishBeanFactoryInitialization 方法:

    1. protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
    2. // Initialize conversion service for this context.
    3. if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
    4. beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
    5. beanFactory.setConversionService(
    6. beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
    7. }
    8. // Register a default embedded value resolver if no bean post-processor
    9. // (such as a PropertyPlaceholderConfigurer bean) registered any before:
    10. // at this point, primarily for resolution in annotation attribute values.
    11. if (!beanFactory.hasEmbeddedValueResolver()) {
    12. beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
    13. }
    14. // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
    15. String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
    16. for (String weaverAwareName : weaverAwareNames) {
    17. getBean(weaverAwareName);
    18. }
    19. // Stop using the temporary ClassLoader for type matching.
    20. beanFactory.setTempClassLoader(null);
    21. // Allow for caching all bean definition metadata, not expecting further changes.
    22. beanFactory.freezeConfiguration();
    23. // Instantiate all remaining (non-lazy-init) singletons.
    24. beanFactory.preInstantiateSingletons();
    25. }

    2.5.2 源码流程分析

  • AbstractApplicationContext 类的 finishBeanFactoryInitialization()

    • 为上下文初始化类型转换器(核心类:ConversionService)
    • 设置默认值解析器
    • AOP相关内容
    • 实例化单例Bean

      1. /**
      2. * Finish the initialization of this context's bean factory,
      3. * initializing all remaining singleton beans.
      4. */
      5. protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
      6. // Initialize conversion service for this context.
      7. // 为上下文初始化类型转换器
      8. if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) && beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
      9. beanFactory.setConversionService(beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
      10. }
      11. // Register a default embedded value resolver if no bean post-processor
      12. // (such as a PropertyPlaceholderConfigurer bean) registered any before:
      13. // at this point, primarily for resolution in annotation attribute values.
      14. // 如果beanFactory 之前没有注册嵌入值解析器,则注册默认的嵌入值解析器,主要用于注解属性值的解析
      15. if (!beanFactory.hasEmbeddedValueResolver()) {
      16. beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
      17. }
      18. // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
      19. String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
      20. for (String weaverAwareName : weaverAwareNames) {
      21. getBean(weaverAwareName);
      22. }
      23. // Stop using the temporary ClassLoader for type matching.
      24. beanFactory.setTempClassLoader(null);
      25. // Allow for caching all bean definition metadata, not expecting further changes.
      26. // 实例化前禁止修改bean 中的属性,允许缓存但是不允许改变
      27. beanFactory.freezeConfiguration();
      28. // Instantiate all remaining (non-lazy-init) singletons.
      29. // 实例化单例bean
      30. beanFactory.preInstantiateSingletons();
      31. }
  • DefaultListableBeanFactory 类中的 preInstantiateSingletons()方法

    • 从解析好的BeanDefinition集合中获取需要进行初始化的对象
    • 通过 getBean()方法中的流程进行bean 实例的初始化,注意区分 beanFactory 和FactoryBean 两种实例化方式都会在这里发生,但是具体的方式是不同的

      1. public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
      2. implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
      3. @Override
      4. public void preInstantiateSingletons() throws BeansException {
      5. if (logger.isDebugEnabled()) {
      6. logger.debug("Pre-instantiating singletons in " + this);
      7. }
      8. // Iterate over a copy to allow for init methods which in turn register new bean definitions.
      9. // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
      10. // 将所有的BeanDefinition的名称创建一个集合
      11. List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
      12. // Trigger initialization of all non-lazy singleton beans...
      13. // 触发所有非延迟加载单例bean 的初始化,遍历集合对象
      14. for (String beanName : beanNames) {
      15. // 合并父类 BeanDefinition
      16. // 判断缓存中是否存在,如果存在直接返回,不存在创建 RootBeanDefinition()
      17. RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
      18. // 条件判断 抽象、单列、懒加载
      19. if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
      20. // FactoryBean & BeanFactory
      21. // 1. 都是对象工厂, 用来创建对象
      22. // 2. 如果使用BeanFactory的接口,那么必须要严格遵守Spring bean 的生命周期,从实例化到初始化,到后面的一系列方法都必须执行
      23. // 3. 创建的对象都会交给Spring来管理: beanFactory 创建的对象会放入一级缓存 singltonObjects; FactoryBean 创建对象放入了 factoryBeanObjectCache中
      24. // 4. 如果不是单列的话,那么需要每次调用的时候重新创建,缓存中不会保存当前对象
      25. // 如果需要一种更加便捷的方式创建,所以就有了FactoryBean, 不需要遵守Spring bean 的生命周期;甚至可以在里面通过 new 创建对象
      26. // 当使用 FactoryBean 接口来创建对象的时候,一共创建了2个对象
      27. // 1. 实现了factoryBean 接口的子类对象 2. 通过getObject方法返回的对象
      28. // 判断是否实现了factoryBean 接口
      29. if (isFactoryBean(beanName)) {
      30. Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
      31. if (bean instanceof FactoryBean) {
      32. final FactoryBean<?> factory = (FactoryBean<?>) bean;
      33. boolean isEagerInit;
      34. if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
      35. isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
      36. ((SmartFactoryBean<?>) factory)::isEagerInit,
      37. getAccessControlContext());
      38. }
      39. else {
      40. isEagerInit = (factory instanceof SmartFactoryBean &&
      41. ((SmartFactoryBean<?>) factory).isEagerInit());
      42. }
      43. if (isEagerInit) {
      44. getBean(beanName);
      45. }
      46. }
      47. }
      48. else {
      49. getBean(beanName);
      50. }
      51. }
      52. }
      53. // Trigger post-initialization callback for all applicable beans...
      54. for (String beanName : beanNames) {
      55. Object singletonInstance = getSingleton(beanName);
      56. if (singletonInstance instanceof SmartInitializingSingleton) {
      57. final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
      58. if (System.getSecurityManager() != null) {
      59. AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
      60. smartSingleton.afterSingletonsInstantiated();
      61. return null;
      62. }, getAccessControlContext());
      63. }
      64. else {
      65. smartSingleton.afterSingletonsInstantiated();
      66. }
      67. }
      68. }
      69. }
      70. }

2.6 获取bean流程(实例对象流程)

基本流程:
image.png

2.7 调用后置处理器解析BeanDefiniiton(invokeBeanFactoryPostProcessors)

  • 13个方法中的 invokeBeanFactoryPostProcessors方法

    1. @Override
    2. public void refresh() throws BeansException, IllegalStateException {
    3. synchronized (this.startupShutdownMonitor) {
    4. ........
    5. try {
    6. // Allows post-processing of the bean factory in context subclasses.
    7. // 子类覆盖方法做额外的处理,此处我们自己一般不做任何扩展工作,但是可以查看web中的代码,是有具体实现的
    8. postProcessBeanFactory(beanFactory);
    9. // Invoke factory processors registered as beans in the context.
    10. // 调用各种beanFactory处理器
    11. invokeBeanFactoryPostProcessors(beanFactory);
    12. // Register bean processors that intercept bean creation.
    13. // 注册bean处理器,这里只是注册功能,真正调用的是getBean方法
    14. registerBeanPostProcessors(beanFactory);
    15. // Initialize message source for this context.
    16. // 为上下文初始化message源,即不同语言的消息体,国际化处理,在springmvc的时候通过国际化的代码重点讲
    17. initMessageSource();
    18. ........
    19. }
    20. ........
    21. }
  • AbstractApplicationContext类中的 invokeBeanFactoryPostProcessors方法

    1. /**
    2. * 实例化并且调用所有已经注册了的beanFactoryPostProcessor,遵循指明的顺序
    3. *
    4. * Instantiate and invoke all registered BeanFactoryPostProcessor beans,
    5. * respecting explicit order if given.
    6. * <p>Must be called before singleton instantiation.
    7. */
    8. protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    9. // 获取到当前应用程序上下文的beanFactoryPostProcessors变量的值,并且实例化调用执行所有已经注册的beanFactoryPostProcessor
    10. // 默认情况下,通过getBeanFactoryPostProcessors()来获取已经注册的BFPP,但是默认是空的,那么问题来了,如果你想扩展,怎么进行扩展工作?
    11. PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
    12. // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
    13. // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
    14. if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
    15. beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
    16. beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
    17. }
    18. }
  • PostProcessorRegistrationDelegate 类中的 invokeBeanFactoryPostProcessors() 方法

    1. public static void invokeBeanFactoryPostProcessors(
    2. ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
    3. // Invoke BeanDefinitionRegistryPostProcessors first, if any.
    4. // 无论是什么情况,优先执行BeanDefinitionRegistryPostProcessors
    5. // 将已经执行过的BFPP存储在processedBeans中,防止重复执行
    6. Set<String> processedBeans = new HashSet<>();
    7. // 判断beanfactory是否是BeanDefinitionRegistry类型,此处是DefaultListableBeanFactory,实现了BeanDefinitionRegistry接口,所以为true
    8. if (beanFactory instanceof BeanDefinitionRegistry) {
    9. // 类型转换
    10. BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
    11. // 此处希望大家做一个区分,两个接口是不同的,BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子集
    12. // BeanFactoryPostProcessor主要针对的操作对象是BeanFactory,而BeanDefinitionRegistryPostProcessor主要针对的操作对象是BeanDefinition
    13. // 存放BeanFactoryPostProcessor的集合
    14. List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
    15. // 存放BeanDefinitionRegistryPostProcessor的集合
    16. List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
    17. // 首先处理入参中的beanFactoryPostProcessors,遍历所有的beanFactoryPostProcessors,将BeanDefinitionRegistryPostProcessor
    18. // 和BeanFactoryPostProcessor区分开
    19. for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
    20. // 如果是BeanDefinitionRegistryPostProcessor
    21. if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
    22. BeanDefinitionRegistryPostProcessor registryProcessor =
    23. (BeanDefinitionRegistryPostProcessor) postProcessor;
    24. // 直接执行BeanDefinitionRegistryPostProcessor接口中的postProcessBeanDefinitionRegistry方法
    25. registryProcessor.postProcessBeanDefinitionRegistry(registry);
    26. // 添加到registryProcessors,用于后续执行postProcessBeanFactory方法
    27. registryProcessors.add(registryProcessor);
    28. } else {
    29. // 否则,只是普通的BeanFactoryPostProcessor,添加到regularPostProcessors,用于后续执行postProcessBeanFactory方法
    30. regularPostProcessors.add(postProcessor);
    31. }
    32. }
    33. // Do not initialize FactoryBeans here: We need to leave all regular beans
    34. // uninitialized to let the bean factory post-processors apply to them!
    35. // Separate between BeanDefinitionRegistryPostProcessors that implement
    36. // PriorityOrdered, Ordered, and the rest.
    37. // 用于保存本次要执行的BeanDefinitionRegistryPostProcessor
    38. List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
    39. // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
    40. // 调用所有实现PriorityOrdered接口的BeanDefinitionRegistryPostProcessor实现类
    41. // 找到所有实现BeanDefinitionRegistryPostProcessor接口bean的beanName
    42. String[] postProcessorNames =
    43. beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
    44. // 遍历处理所有符合规则的postProcessorNames
    45. for (String ppName : postProcessorNames) {
    46. // 检测是否实现了PriorityOrdered接口
    47. if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
    48. // 获取名字对应的bean实例,添加到currentRegistryProcessors中
    49. currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
    50. // 将要被执行的BFPP名称添加到processedBeans,避免后续重复执行
    51. processedBeans.add(ppName);
    52. }
    53. }
    54. // 按照优先级进行排序操作
    55. sortPostProcessors(currentRegistryProcessors, beanFactory);
    56. // 添加到registryProcessors中,用于最后执行postProcessBeanFactory方法
    57. registryProcessors.addAll(currentRegistryProcessors);
    58. // 遍历currentRegistryProcessors,执行postProcessBeanDefinitionRegistry方法
    59. invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
    60. // 执行完毕之后,清空currentRegistryProcessors
    61. currentRegistryProcessors.clear();
    62. // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
    63. // 调用所有实现Ordered接口的BeanDefinitionRegistryPostProcessor实现类
    64. // 找到所有实现BeanDefinitionRegistryPostProcessor接口bean的beanName,
    65. // 此处需要重复查找的原因在于上面的执行过程中可能会新增其他的BeanDefinitionRegistryPostProcessor
    66. postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
    67. for (String ppName : postProcessorNames) {
    68. // 检测是否实现了Ordered接口,并且还未执行过
    69. if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
    70. // 获取名字对应的bean实例,添加到currentRegistryProcessors中
    71. currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
    72. // 将要被执行的BFPP名称添加到processedBeans,避免后续重复执行
    73. processedBeans.add(ppName);
    74. }
    75. }
    76. // 按照优先级进行排序操作
    77. sortPostProcessors(currentRegistryProcessors, beanFactory);
    78. // 添加到registryProcessors中,用于最后执行postProcessBeanFactory方法
    79. registryProcessors.addAll(currentRegistryProcessors);
    80. // 遍历currentRegistryProcessors,执行postProcessBeanDefinitionRegistry方法
    81. invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
    82. // 执行完毕之后,清空currentRegistryProcessors
    83. currentRegistryProcessors.clear();
    84. // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
    85. // 最后,调用所有剩下的BeanDefinitionRegistryPostProcessors
    86. boolean reiterate = true;
    87. while (reiterate) {
    88. reiterate = false;
    89. // 找出所有实现BeanDefinitionRegistryPostProcessor接口的类
    90. postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
    91. // 遍历执行
    92. for (String ppName : postProcessorNames) {
    93. // 跳过已经执行过的BeanDefinitionRegistryPostProcessor
    94. if (!processedBeans.contains(ppName)) {
    95. // 获取名字对应的bean实例,添加到currentRegistryProcessors中
    96. currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
    97. // 将要被执行的BFPP名称添加到processedBeans,避免后续重复执行
    98. processedBeans.add(ppName);
    99. reiterate = true;
    100. }
    101. }
    102. // 按照优先级进行排序操作
    103. sortPostProcessors(currentRegistryProcessors, beanFactory);
    104. // 添加到registryProcessors中,用于最后执行postProcessBeanFactory方法
    105. registryProcessors.addAll(currentRegistryProcessors);
    106. // 遍历currentRegistryProcessors,执行postProcessBeanDefinitionRegistry方法
    107. invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
    108. // 执行完毕之后,清空currentRegistryProcessors
    109. currentRegistryProcessors.clear();
    110. }
    111. // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
    112. // 调用所有BeanDefinitionRegistryPostProcessor的postProcessBeanFactory方法
    113. invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
    114. // 最后,调用入参beanFactoryPostProcessors中的普通BeanFactoryPostProcessor的postProcessBeanFactory方法
    115. invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
    116. } else {
    117. // Invoke factory processors registered with the context instance.
    118. // 如果beanFactory不归属于BeanDefinitionRegistry类型,那么直接执行postProcessBeanFactory方法
    119. invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
    120. }
    121. // 到这里为止,入参beanFactoryPostProcessors和容器中的所有BeanDefinitionRegistryPostProcessor已经全部处理完毕,下面开始处理容器中
    122. // 所有的BeanFactoryPostProcessor
    123. // 可能会包含一些实现类,只实现了BeanFactoryPostProcessor,并没有实现BeanDefinitionRegistryPostProcessor接口
    124. // Do not initialize FactoryBeans here: We need to leave all regular beans
    125. // uninitialized to let the bean factory post-processors apply to them!
    126. // 找到所有实现BeanFactoryPostProcessor接口的类
    127. String[] postProcessorNames =
    128. beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
    129. // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
    130. // Ordered, and the rest.
    131. // 用于存放实现了PriorityOrdered接口的BeanFactoryPostProcessor
    132. List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
    133. // 用于存放实现了Ordered接口的BeanFactoryPostProcessor的beanName
    134. // List<String> orderedPostProcessorNames = new ArrayList<>();
    135. List<BeanFactoryPostProcessor> orderedPostProcessor = new ArrayList<>();
    136. // 用于存放普通BeanFactoryPostProcessor的beanName
    137. // List<String> nonOrderedPostProcessorNames = new ArrayList<>();
    138. List<BeanFactoryPostProcessor> nonOrderedPostProcessorNames = new ArrayList<>();
    139. // 遍历postProcessorNames,将BeanFactoryPostProcessor按实现PriorityOrdered、实现Ordered接口、普通三种区分开
    140. for (String ppName : postProcessorNames) {
    141. // 跳过已经执行过的BeanFactoryPostProcessor
    142. if (processedBeans.contains(ppName)) {
    143. // skip - already processed in first phase above
    144. }
    145. // 添加实现了PriorityOrdered接口的BeanFactoryPostProcessor到priorityOrderedPostProcessors
    146. else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
    147. priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
    148. }
    149. // 添加实现了Ordered接口的BeanFactoryPostProcessor的beanName到orderedPostProcessorNames
    150. else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
    151. // orderedPostProcessorNames.add(ppName);
    152. orderedPostProcessor.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
    153. } else {
    154. // 添加剩下的普通BeanFactoryPostProcessor的beanName到nonOrderedPostProcessorNames
    155. // nonOrderedPostProcessorNames.add(ppName);
    156. nonOrderedPostProcessorNames.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
    157. }
    158. }
    159. // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
    160. // 对实现了PriorityOrdered接口的BeanFactoryPostProcessor进行排序
    161. sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
    162. // 遍历实现了PriorityOrdered接口的BeanFactoryPostProcessor,执行postProcessBeanFactory方法
    163. invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
    164. // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
    165. // 创建存放实现了Ordered接口的BeanFactoryPostProcessor集合
    166. // List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
    167. // 遍历存放实现了Ordered接口的BeanFactoryPostProcessor名字的集合
    168. // for (String postProcessorName : orderedPostProcessorNames) {
    169. // 将实现了Ordered接口的BeanFactoryPostProcessor添加到集合中
    170. // orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    171. // }
    172. // 对实现了Ordered接口的BeanFactoryPostProcessor进行排序操作
    173. // sortPostProcessors(orderedPostProcessors, beanFactory);
    174. sortPostProcessors(orderedPostProcessor, beanFactory);
    175. // 遍历实现了Ordered接口的BeanFactoryPostProcessor,执行postProcessBeanFactory方法
    176. // invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
    177. invokeBeanFactoryPostProcessors(orderedPostProcessor, beanFactory);
    178. // Finally, invoke all other BeanFactoryPostProcessors.
    179. // 最后,创建存放普通的BeanFactoryPostProcessor的集合
    180. // List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
    181. // 遍历存放实现了普通BeanFactoryPostProcessor名字的集合
    182. // for (String postProcessorName : nonOrderedPostProcessorNames) {
    183. // 将普通的BeanFactoryPostProcessor添加到集合中
    184. // nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    185. // }
    186. // 遍历普通的BeanFactoryPostProcessor,执行postProcessBeanFactory方法
    187. // invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
    188. invokeBeanFactoryPostProcessors(nonOrderedPostProcessorNames, beanFactory);
    189. // Clear cached merged bean definitions since the post-processors might have
    190. // modified the original metadata, e.g. replacing placeholders in values...
    191. // 清除元数据缓存(mergeBeanDefinitions、allBeanNamesByType、singletonBeanNameByType)
    192. // 因为后置处理器可能已经修改了原始元数据,例如,替换值中的占位符
    193. beanFactory.clearMetadataCache();
    194. }
  • 调用BeanDefinitionRegistryPostProcessor 类的postProcessBeanDefinitionRegistry()方法

    • BeanDefinitionRegistryPostProcessor 类继承了BeanFactoryPostProcessor
    • 具体的方法有具体的实现类 ConfigurationClassPostProcessor 类去执行
    • 此流程总出现了最核心的后置处理器ConfigurationClassPostProcessor类

      1. /**
      2. * 定位、加载、解析、注册相关注解
      3. *
      4. * Derive further bean definitions from the configuration classes in the registry.
      5. */
      6. @Override
      7. public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
      8. // 根据对应的registry对象生成hashcode值,此对象只会操作一次,如果之前处理过则抛出异常
      9. int registryId = System.identityHashCode(registry);
      10. if (this.registriesPostProcessed.contains(registryId)) {
      11. throw new IllegalStateException(
      12. "postProcessBeanDefinitionRegistry already called on this post-processor against " + registry);
      13. }
      14. if (this.factoriesPostProcessed.contains(registryId)) {
      15. throw new IllegalStateException(
      16. "postProcessBeanFactory already called on this post-processor against " + registry);
      17. }
      18. // 将马上要进行处理的registry对象的id值放到已经处理的集合对象中
      19. this.registriesPostProcessed.add(registryId);
      20. // 处理配置类的bean定义信息
      21. processConfigBeanDefinitions(registry);
      22. }
  • ConfigurationClassPostProcessor 类中的processConfigBeanDefinition(BeanDefinitionRegistry)方法执行

    • 从DefaultListableBeanFactory 容器中获取所有的beanDefinitionNames,通过名称获取BeanDefinition对象
    • 循环遍历 BeanDefiniiton对象是否被@Configuration 注解修饰
      • 先判断是否被@Configuration 注解修饰

主流程代码:

  1. /**
  2. * 构建和验证一个类是否被@Configuration修饰,并做相关的解析工作
  3. *
  4. * 如果你对此方法了解清楚了,那么springboot的自动装配原理就清楚了
  5. *
  6. * Build and validate a configuration model based on the registry of
  7. * {@link Configuration} classes.
  8. */
  9. public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
  10. // 创建存放BeanDefinitionHolder的对象集合
  11. List<BeanDefinitionHolder> configCandidates = new ArrayList<>();
  12. // 当前registry就是DefaultListableBeanFactory,获取所有已经注册的BeanDefinition的beanName
  13. String[] candidateNames = registry.getBeanDefinitionNames();
  14. // 遍历所有要处理的beanDefinition的名称,筛选对应的beanDefinition(被注解修饰的)
  15. for (String beanName : candidateNames) {
  16. // 获取指定名称的BeanDefinition对象
  17. BeanDefinition beanDef = registry.getBeanDefinition(beanName);
  18. // 如果beanDefinition中的configurationClass属性不等于空,那么意味着已经处理过,输出日志信息
  19. if (beanDef.getAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE) != null) {
  20. if (logger.isDebugEnabled()) {
  21. logger.debug("Bean definition has already been processed as a configuration class: " + beanDef);
  22. }
  23. }
  24. // 判断当前BeanDefinition是否是一个配置类,并为BeanDefinition设置属性为lite或者full,此处设置属性值是为了后续进行调用
  25. // 如果Configuration配置proxyBeanMethods代理为true则为full
  26. // 如果加了@Bean、@Component、@ComponentScan、@Import、@ImportResource注解,则设置为lite
  27. // 如果配置类上被@Order注解标注,则设置BeanDefinition的order属性值
  28. else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {
  29. // 添加到对应的集合对象中
  30. configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));
  31. }
  32. }
  33. // Return immediately if no @Configuration classes were found
  34. // 如果没有发现任何配置类,则直接返回
  35. if (configCandidates.isEmpty()) {
  36. return;
  37. }
  38. // Sort by previously determined @Order value, if applicable
  39. // 如果适用,则按照先前确定的@Order的值排序
  40. configCandidates.sort((bd1, bd2) -> {
  41. int i1 = ConfigurationClassUtils.getOrder(bd1.getBeanDefinition());
  42. int i2 = ConfigurationClassUtils.getOrder(bd2.getBeanDefinition());
  43. return Integer.compare(i1, i2);
  44. });
  45. // Detect any custom bean name generation strategy supplied through the enclosing application context
  46. // 判断当前类型是否是SingletonBeanRegistry类型
  47. SingletonBeanRegistry sbr = null;
  48. if (registry instanceof SingletonBeanRegistry) {
  49. // 类型的强制转换
  50. sbr = (SingletonBeanRegistry) registry;
  51. // 判断是否有自定义的beanName生成器
  52. if (!this.localBeanNameGeneratorSet) {
  53. // 获取自定义的beanName生成器
  54. BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(
  55. AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR);
  56. // 如果有自定义的命名生成策略
  57. if (generator != null) {
  58. //设置组件扫描的beanName生成策略
  59. this.componentScanBeanNameGenerator = generator;
  60. // 设置import bean name生成策略
  61. this.importBeanNameGenerator = generator;
  62. }
  63. }
  64. }
  65. // 如果环境对象等于空,那么就重新创建新的环境对象
  66. if (this.environment == null) {
  67. this.environment = new StandardEnvironment();
  68. }
  69. // Parse each @Configuration class
  70. // 实例化ConfigurationClassParser类,并初始化相关的参数,完成配置类的解析工作
  71. ConfigurationClassParser parser = new ConfigurationClassParser(
  72. this.metadataReaderFactory, this.problemReporter, this.environment,
  73. this.resourceLoader, this.componentScanBeanNameGenerator, registry);
  74. // 创建两个集合对象,
  75. // 存放相关的BeanDefinitionHolder对象
  76. Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);
  77. // 存放扫描包下的所有bean
  78. Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size());
  79. do {
  80. // 解析带有@Controller、@Import、@ImportResource、@ComponentScan、@ComponentScans、@Bean的BeanDefinition
  81. parser.parse(candidates);
  82. // 将解析完的Configuration配置类进行校验,1、配置类不能是final,2、@Bean修饰的方法必须可以重写以支持CGLIB
  83. parser.validate();
  84. // 获取所有的bean,包括扫描的bean对象,@Import导入的bean对象
  85. Set<ConfigurationClass> configClasses = new LinkedHashSet<>(parser.getConfigurationClasses());
  86. // 清除掉已经解析处理过的配置类
  87. configClasses.removeAll(alreadyParsed);
  88. // Read the model and create bean definitions based on its content
  89. // 判断读取器是否为空,如果为空的话,就创建完全填充好的ConfigurationClass实例的读取器
  90. if (this.reader == null) {
  91. this.reader = new ConfigurationClassBeanDefinitionReader(
  92. registry, this.sourceExtractor, this.resourceLoader, this.environment,
  93. this.importBeanNameGenerator, parser.getImportRegistry());
  94. }
  95. // 核心方法,将完全填充好的ConfigurationClass实例转化为BeanDefinition注册入IOC容器
  96. this.reader.loadBeanDefinitions(configClasses);
  97. // 添加到已经处理的集合中
  98. alreadyParsed.addAll(configClasses);
  99. candidates.clear();
  100. // 这里判断registry.getBeanDefinitionCount() > candidateNames.length的目的是为了知道reader.loadBeanDefinitions(configClasses)这一步有没有向BeanDefinitionMap中添加新的BeanDefinition
  101. // 实际上就是看配置类(例如AppConfig类会向BeanDefinitionMap中添加bean)
  102. // 如果有,registry.getBeanDefinitionCount()就会大于candidateNames.length
  103. // 这样就需要再次遍历新加入的BeanDefinition,并判断这些bean是否已经被解析过了,如果未解析,需要重新进行解析
  104. // 这里的AppConfig类向容器中添加的bean,实际上在parser.parse()这一步已经全部被解析了
  105. if (registry.getBeanDefinitionCount() > candidateNames.length) {
  106. String[] newCandidateNames = registry.getBeanDefinitionNames();
  107. Set<String> oldCandidateNames = new HashSet<>(Arrays.asList(candidateNames));
  108. Set<String> alreadyParsedClasses = new HashSet<>();
  109. for (ConfigurationClass configurationClass : alreadyParsed) {
  110. alreadyParsedClasses.add(configurationClass.getMetadata().getClassName());
  111. }
  112. // 如果有未解析的类,则将其添加到candidates中,这样candidates不为空,就会进入到下一次的while的循环中
  113. for (String candidateName : newCandidateNames) {
  114. if (!oldCandidateNames.contains(candidateName)) {
  115. BeanDefinition bd = registry.getBeanDefinition(candidateName);
  116. if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) &&
  117. !alreadyParsedClasses.contains(bd.getBeanClassName())) {
  118. candidates.add(new BeanDefinitionHolder(bd, candidateName));
  119. }
  120. }
  121. }
  122. candidateNames = newCandidateNames;
  123. }
  124. }
  125. while (!candidates.isEmpty());
  126. // Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes
  127. if (sbr != null && !sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) {
  128. sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry());
  129. }
  130. if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) {
  131. // Clear cache in externally provided MetadataReaderFactory; this is a no-op
  132. // for a shared cache since it'll be cleared by the ApplicationContext.
  133. ((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache();
  134. }
  135. }
  • 判断是否被注解修饰流程

    1. - **判断是否被@Configuration 修饰**
    1. if (isFullConfigurationCandidate(metadata)) {
    2. beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
    3. }
    4. public static boolean isFullConfigurationCandidate(AnnotationMetadata metadata) {
    5. return metadata.isAnnotated(Configuration.class.getName());
    6. }
    1. - **如果没有,判断@Import@ImportResource@ComponentScan@ComponentScans**
    2. - **如果没有,判断 @Bean **
    1. public static boolean isLiteConfigurationCandidate(AnnotationMetadata metadata) {
    2. // Do not consider an interface or an annotation...
    3. if (metadata.isInterface()) {
    4. return false;
    5. }
    6. // Any of the typical annotations found?
    7. for (String indicator : candidateIndicators) {
    8. if (metadata.isAnnotated(indicator)) {
    9. return true;
    10. }
    11. }
    12. // Finally, let's look for @Bean methods...
    13. try {
    14. return metadata.hasAnnotatedMethods(Bean.class.getName());
    15. }
    16. catch (Throwable ex) {
    17. if (logger.isDebugEnabled()) {
    18. logger.debug("Failed to introspect @Bean methods on class [" + metadata.getClassName() + "]: " + ex);
    19. }
    20. return false;
    21. }
    22. }



五、AOP

  1. AOP-面向切面编程

一、AOP设计原理

1.1 Java 程序在JVM 中运行特征

当我们在某个类中写了一个main()方法,然后开始执行程序,整个程序完成运行。如下:

  1. public static void main(String[] args) {
  2. String result = new BCryptPasswordEncoder().encode("lego_bi");
  3. System.out.println(result);
  4. }

那么在这个执行的工程中,JVM 做了什么?
当我们执行程序的时候,JVM 会创建一个主线程main,这个主线程将以上述的main()方法作为入口,开始执行代码。每个线程在自己的内在中维护着属于自己的栈(Stack),记录着整个程序的运行过程。栈里的每个元素被称为栈帧(Stack Frame),栈帧表示着某个方法调用,会记录方法调用的信息;实际上我们在代码中调用一个方法的时候,在内存中就对应着一个栈帧的入栈和出栈。

在某个特定的时间范围内,一个Main 线程中的栈变化情况如下图:
image.png

从线程栈的角度来看,我们可以看到,JVM处理Java程序的基本单位是方法调用。实际上,JVM执行的最基本单位的指令(即原子操作)是汇编语言性质的机器字节码。这里之所以讲方法调用时Java程序的基本执行单位,是从更宏观的角度看待的

1.2 Java 程序执行流【了解AOP 中的连接点(Join point)、切入点(point cut) 的概念】

如果从虚拟机栈角度考虑Java 程序执行的话,我们会发现,整个程序执行就是方法调用的过程。将方法执行的顺序,将方法调用排成一排,这样就构成了Java 程序执行流。
我们将Java 程序执行流简单画个图如下:
image.png

基于时间序列,我们可以将方法调用排成一条线。而每个方法调用则可以看成Java执行流中的一个节点。这个节点在AOP的术语中,被称为Join Point,即连接点。 一个Java程序的运行的过程,就是若干个连接点连接起来依次执行的过程。
在我们正常的面向对象的思维中, 我们考虑的是如何按照时间序列通过方法调用来实现我们的业务逻辑。那么,什么是AOP(即面向切面的编程)呢?
通常面向对象的程序,代码都是按照时间序列纵向展开的,而他们都有一个共性:即都是已方法调用作为基本执行单位展开的。 将方法调用当做一个连接点,那么由连接点串起来的程序执行流就是整个程序的执行过程。
AOP(Aspect Oriented Programming)则是从另外一个角度来考虑整个程序的,AOP将每一个方法调用,即连接点作为编程的入口,针对方法调用进行编程。从执行的逻辑上来看,相当于在之前纵向的按照时间轴执行的程序横向切入。相当于将之前的程序横向切割成若干的面,即Aspect.每个面被称为切面。 所以,根据我的理解,AOP本质上是针对方法调用的编程思路。

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12747730/1638325409552-b02f61c9-8967-43ba-b985-4f3c9aa24fb7.png#clientId=uc7864c60-66a5-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=414&id=uf4ae8215&margin=%5Bobject%20Object%5D&name=image.png&originHeight=827&originWidth=693&originalType=binary&ratio=1&rotation=0&showTitle=false&size=241008&status=done&style=none&taskId=uccc51af3-f56b-41f2-aed5-b7a1f8f99c0&title=&width=346.5)

既然AOP是针对切面进行的编程的,那么,你需要选择哪些切面(即 连接点Joint Point)作为你的编程对象呢?

因为切面本质上是每一个方法调用,选择切面的过程实际上就是选择方法的过程。那么,被选择的切面(Aspect)在AOP术语里被称为切入点(Point Cut). 切入点实际上也是从所有的连接点(Join point)挑选自己感兴趣的连接点的过程。

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12747730/1638326480637-af622aa7-6a9d-4d95-850e-8d326e9ec724.png#clientId=uc7864c60-66a5-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=362&id=ub3f99c30&margin=%5Bobject%20Object%5D&name=image.png&originHeight=723&originWidth=688&originalType=binary&ratio=1&rotation=0&showTitle=false&size=205666&status=done&style=none&taskId=ua3d28851-641d-4c1a-b49d-eda8c471beb&title=&width=344)

1.3 引入代理模式的Java程序执行流(AOP实现的机制)

假设我们在我们的Java 代码里,都为实例对象通过代理模式创建了代理对象,访问这些实例对象必须通过代理,那么加入代理对象的Java程序执行流比较复杂一点
image.png

由上图可以看出,只要想调用某一个实例对象的方法时,都会经过这个实例对象相对应的代理对象, 即执行的控制权先交给代理对象。

关于代理模式:
image.png

加入了代理模式的Java程序执行流,使得所有的方法调用都经过了代理对象。对于Spring AOP框架而言,它负责控制着真个容器内部的代理对象。当我们调用了某一个实例对象的任何一个非final的public方法时,整个Spring框架都会知晓。

此时的SpringAOP框架在某种程度上扮演着一个上帝的角色:它知道你在这个框架内所做的任何操作,你对每一个实例对象的非final的public方法调用都可以被框架察觉到!

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12747730/1638327232209-8cbaaa46-d0d6-4f35-8cea-7d18d3e96ceb.png#clientId=uc7864c60-66a5-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=419&id=uf10809a1&margin=%5Bobject%20Object%5D&name=image.png&originHeight=837&originWidth=648&originalType=binary&ratio=1&rotation=0&showTitle=false&size=190573&status=done&style=none&taskId=u4869dcf4-7e44-46e7-a49b-918503a5c05&title=&width=324)

1.4 AOP的工作原理

AOP编程首先要选择它感兴趣的连接点——即切入点(Point cut),那么,AOP能对切入点做什么样的编程呢? 我们先将代理模式下的某个连接点细化,你会看到如下这个示意图所表示的过程:
image.png

AOP 工作原理

  1. AOP 的核心主要分为三部分:
  2. 1. 哪些类需要进行相关的切入
  3. expression (表达式来找到切入的类) ----->pointCut
  4. 2. 增强的逻辑处理,有几个通知消息或者说哪些逻辑可以被执行
  5. beforeafterafterThrowingafterReturingaround ----> advisor ---> advice
  6. 3. 增强的处理逻辑类是那个,也就是那个切面
  7. aspect
  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12747730/1638327581292-8e532878-c1e2-4e0e-9781-ea3f489f6e9b.png#clientId=uc7864c60-66a5-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=451&id=u8c4c63f5&margin=%5Bobject%20Object%5D&name=image.png&originHeight=902&originWidth=804&originalType=binary&ratio=1&rotation=0&showTitle=false&size=447119&status=done&style=none&taskId=u52496ca1-acb5-4530-b5de-68cbebf5d1c&title=&width=402)

AOP 面向切面编程机制

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12747730/1638327684198-786b7015-ccf9-4259-aeac-9dbd15809c99.png#clientId=uc7864c60-66a5-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=489&id=ub1f7a099&margin=%5Bobject%20Object%5D&name=image.png&originHeight=978&originWidth=811&originalType=binary&ratio=1&rotation=0&showTitle=false&size=299921&status=done&style=none&taskId=ubeb2bb20-dd56-49cb-aaa4-0fb7744f633&title=&width=405.5)

二、AOP实现原理

三、源码分析

aop 相关类继承体系

  • 三个核心类继承关系

    1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12747730/1638621809874-52c407c1-59fc-4e75-b06c-8f68f34e5094.png#clientId=uab9bf88d-07bf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=266&id=ua4d46ad1&margin=%5Bobject%20Object%5D&name=image.png&originHeight=532&originWidth=1103&originalType=binary&ratio=1&rotation=0&showTitle=false&size=55312&status=done&style=none&taskId=ue3a90945-d5a3-4e1d-897b-7a116939eac&title=&width=551.5)
  • advice 相关类

    1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12747730/1638623465306-dcbc3b30-97f4-4f08-bd9d-47da19b56868.png#clientId=uab9bf88d-07bf-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=92&id=u58ffe6da&margin=%5Bobject%20Object%5D&name=image.png&originHeight=183&originWidth=1054&originalType=binary&ratio=1&rotation=0&showTitle=false&size=17270&status=done&style=none&taskId=u0289bff7-e3a7-47da-842c-c654e0723bc&title=&width=527)

  • 3.1 AOP 的BeanDefinition的准备工作

  • AbstractAutouwireCapableBeanFactory 类中的 createBean() 方法中的resolveBeforeInstantiation()方法

    1. public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory
    2. implements AutowireCapableBeanFactory {
    3. /** Strategy for creating bean instances */
    4. // 初始化策略
    5. private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();
    6. /** Resolver strategy for method parameter names */
    7. @Nullable
    8. private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
    9. /** Whether to automatically try to resolve circular references between beans */
    10. private boolean allowCircularReferences = true;
    11. /**
    12. * Whether to resort to injecting a raw bean instance in case of circular reference,
    13. * even if the injected bean eventually got wrapped.
    14. */
    15. private boolean allowRawInjectionDespiteWrapping = false;
    16. /**
    17. * Dependency types to ignore on dependency check and autowire, as Set of
    18. * Class objects: for example, String. Default is none.
    19. */
    20. private final Set<Class<?>> ignoredDependencyTypes = new HashSet<>();
    21. /**
    22. * Dependency interfaces to ignore on dependency check and autowire, as Set of
    23. * Class objects. By default, only the BeanFactory interface is ignored.
    24. */
    25. private final Set<Class<?>> ignoredDependencyInterfaces = new HashSet<>();
    26. /**
    27. * The name of the currently created bean, for implicit dependency registration
    28. * on getBean etc invocations triggered from a user-specified Supplier callback.
    29. */
    30. private final NamedThreadLocal<String> currentlyCreatedBean = new NamedThreadLocal<>("Currently created bean");
    31. /** Cache of unfinished FactoryBean instances: FactoryBean name to BeanWrapper */
    32. private final ConcurrentMap<String, BeanWrapper> factoryBeanInstanceCache = new ConcurrentHashMap<>(16);
    33. /** Cache of filtered PropertyDescriptors: bean Class to PropertyDescriptor array */
    34. private final ConcurrentMap<Class<?>, PropertyDescriptor[]> filteredPropertyDescriptorsCache =
    35. new ConcurrentHashMap<>(256);
    36. /**
    37. * Central method of this class: creates a bean instance,
    38. * populates the bean instance, applies post-processors, etc.
    39. * @see #doCreateBean
    40. */
    41. @Override
    42. protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
    43. throws BeanCreationException {
    44. if (logger.isDebugEnabled()) {
    45. logger.debug("Creating instance of bean '" + beanName + "'");
    46. }
    47. RootBeanDefinition mbdToUse = mbd;
    48. // Make sure bean class is actually resolved at this point, and
    49. // clone the bean definition in case of a dynamically resolved Class
    50. // which cannot be stored in the shared merged bean definition.
    51. Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
    52. if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
    53. mbdToUse = new RootBeanDefinition(mbd);
    54. mbdToUse.setBeanClass(resolvedClass);
    55. }
    56. // Prepare method overrides.
    57. try {
    58. mbdToUse.prepareMethodOverrides();
    59. }
    60. catch (BeanDefinitionValidationException ex) {
    61. throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
    62. beanName, "Validation of method overrides failed", ex);
    63. }
    64. try {
    65. // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
    66. // 给BeanPostProcessors一个机会来返回代理来真正的实例,应用实例化的前置处理器
    67. Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
    68. if (bean != null) {
    69. return bean;
    70. }
    71. }
    72. catch (Throwable ex) {
    73. throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
    74. "BeanPostProcessor before instantiation of bean failed", ex);
    75. }
    76. try {
    77. Object beanInstance = doCreateBean(beanName, mbdToUse, args);
    78. if (logger.isDebugEnabled()) {
    79. logger.debug("Finished creating instance of bean '" + beanName + "'");
    80. }
    81. return beanInstance;
    82. }
    83. catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
    84. // A previously detected exception with proper bean creation context already,
    85. // or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
    86. throw ex;
    87. }
    88. catch (Throwable ex) {
    89. throw new BeanCreationException(
    90. mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
    91. }
    92. }
    93. }
  • resolveBeforeInstantiation() 方法中的applyBeanPostProcessorsBeforeInstantiation() 方法

    1. /**
    2. * Apply before-instantiation post-processors, resolving whether there is a
    3. * before-instantiation shortcut for the specified bean.
    4. * @param beanName the name of the bean
    5. * @param mbd the bean definition for the bean
    6. * @return the shortcut-determined bean instance, or {@code null} if none
    7. */
    8. @Nullable
    9. protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
    10. Object bean = null;
    11. if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
    12. // Make sure bean class is actually resolved at this point.
    13. if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
    14. Class<?> targetType = determineTargetType(beanName, mbd);
    15. if (targetType != null) {
    16. bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
    17. if (bean != null) {
    18. bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
    19. }
    20. }
    21. }
    22. mbd.beforeInstantiationResolved = (bean != null);
    23. }
    24. return bean;
    25. }
  • applyBeanPostProcessorsBeforeInstantiation() 方法中的postProcessBeforeInstantiation()方法

    1. /**
    2. * Apply InstantiationAwareBeanPostProcessors to the specified bean definition
    3. * (by class and name), invoking their {@code postProcessBeforeInstantiation} methods.
    4. * <p>Any returned object will be used as the bean instead of actually instantiating
    5. * the target bean. A {@code null} return value from the post-processor will
    6. * result in the target bean being instantiated.
    7. * @param beanClass the class of the bean to be instantiated
    8. * @param beanName the name of the bean
    9. * @return the bean object to use instead of a default instance of the target bean, or {@code null}
    10. * @see InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation
    11. */
    12. @Nullable
    13. protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
    14. for (BeanPostProcessor bp : getBeanPostProcessors()) {
    15. if (bp instanceof InstantiationAwareBeanPostProcessor) {
    16. InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
    17. Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
    18. if (result != null) {
    19. return result;
    20. }
    21. }
    22. }
    23. return null;
    24. }
  • postProcessBeforeInstantiation() 方法的实现类 AbstractAutoProxyCreator中的核心方法createProxy()

image.png

  1. @Override
  2. public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
  3. Object cacheKey = getCacheKey(beanClass, beanName);
  4. if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
  5. if (this.advisedBeans.containsKey(cacheKey)) {
  6. return null;
  7. }
  8. if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
  9. this.advisedBeans.put(cacheKey, Boolean.FALSE);
  10. return null;
  11. }
  12. }
  13. // Create proxy here if we have a custom TargetSource.
  14. // Suppresses unnecessary default instantiation of the target bean:
  15. // The TargetSource will handle target instances in a custom fashion.
  16. TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
  17. if (targetSource != null) {
  18. if (StringUtils.hasLength(beanName)) {
  19. this.targetSourcedBeans.add(beanName);
  20. }
  21. Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
  22. // 创建代理对象
  23. Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
  24. this.proxyTypes.put(cacheKey, proxy.getClass());
  25. return proxy;
  26. }
  27. return null;
  28. }

六、事物

七、Spring 启动流程

STEP1、prepareFresh()

Prepare this context for refreshing: 刷新预处理

STEP2、obtainFreshBeanFactroy()

Tell the subclass to refresh the internal bean factory: 创建并获得beanFactory
主要分为两大步骤:

�1. 创建beanFactory

2. 加载解析XML文件,并完成BeanDefinition的加载和注册工作

loadBeanDefinitions() 基本流程图
image.png

STEP3、prepareBeanFactory(beanFactory)

STEP4、postProcessorBeanFactory(beanFactory)

STEP5、invokePostProcessorBeanFactory(beanFactory)

BeanFactroyPostProcessor 称作为 BeanFactory 增强器,也叫做后置处理器。 在Spring 的流程中,先加载完成配置文件,创建生成一个beanFactroy容器来存放BeanDefinition对象,在BeanDefinition对象被实例化的过程中可能需要对BeanDefiniton 进行增强修改,此时就需要BeanFactroyPostProssor的处理,因此这就是BeanFactroyPostProssor的基本作用。BeanFactroyPostProssor 是一个接口,里面只有一个抽象方法postProcessorBeanFactroy(), BeanFactroyPostProssor 接口有很多字接口实现,进行不同的增强操作,其中最核心的子接口就是ConfigurationClassPostProcessor类,主要解析Spring中的注解完成Bean的创建。
image.png
BFPP和BDRPP 的处理流程代码:

  1. 先执行用户自己添加的PostProcessor
  2. 然后执行实现了BeanDefinitionRegistryPostProcessor 的实现类
  3. 最后执行 实现了BeanFactoryPostProcessor的实现类

    在执行三个PostProcessor 的过程中,都遵循一个规则:PriorityOrder -> order -> 无排序接口
    image.png

STEP6、registerBeanPostProcessors(beanFactory)

STEP7、initMessageSource()

STEP8、initApplicationEventMulticaster()

STEP9、onRefresh()

STEP10、registerListeners()

STEP11、finishBeanFactoryInitialization(beanFactory)

STEP12、finishRefresh()

STEP13、destroyBeans()

STEP14、resetCommonCaches()

八、Spring如何解析注解完成创建Bean 对象?

一、ConfigurationClassPostProcessor入口

  1. AbstractApplicationContext # STEP5 invokePostProcessorBeanFactory
    1. PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors
  2. PostProcessorRegistrationDelegate # 类中的 invokeBeanFactoryPostProcessors()方法
  3. BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry() 方法
  4. 进入到BeanDefinitionRegistryPostProcessor父接口中,根据具体的实现子接口进入到具体的方法
  5. 进入到ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry() 方法中 ```java

    第一步:执行Spring启动流程的第5步

    public void refresh() throws BeansException, IllegalStateException {

    1. synchronized (this.startupShutdownMonitor) {
    2. prepareRefresh();
    3. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    4. prepareBeanFactory(beanFactory);
    5. try {
    6. // Invoke factory processors registered as beans in the context.
    7. //STEP 5: 调用BeanFactoryPostProcessor后置处理器对BeanDefinition处理
    8. // invoke: 调度执行 BeanFactoryPostProcessor
    9. invokeBeanFactoryPostProcessors(beanFactory);

    }

}

第二步:进入到invokePostProcessorBeanFactroy()方法的具体实现

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) { PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors()); // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor) if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } }

第三步:实例化并执行所有已经注册的BeanFactoryPostProcessor beans,

  1. BeanFactoryPostProcessor 的核心执行流程

final class PostProcessorRegistrationDelegate { public static void invokeBeanFactoryPostProcessors( // Invoke BeanDefinitionRegistryPostProcessors first, if any. Set processedBeans = new HashSet<>(); if (beanFactory instanceof BeanDefinitionRegistry) { // 将beanFactory 强转为BeanDefinitionRegisrty 对象 BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; // 创建两个空的对象来设置需要处理的 后置处理器 List regularPostProcessors = new ArrayList<>(); List registryProcessors = new ArrayList<>();

  1. // 将两种类型区分分别添加到创建的空集合中分别处理
  2. for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
  3. if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
  4. BeanDefinitionRegistryPostProcessor registryProcessor =
  5. (BeanDefinitionRegistryPostProcessor) postProcessor;
  6. registryProcessor.postProcessBeanDefinitionRegistry(registry);
  7. registryProcessors.add(registryProcessor);
  8. }
  9. else {
  10. regularPostProcessors.add(postProcessor);
  11. }
  12. }
  13. ........

} }

第四步:BeanDefinitionRegistryPostProcessor 具体实现类执行postProcessBeanDefinitionRegistry()方法

/**

  • 此类是一个后置处理器,主要功能是参与BeanFactory的建造,主要功能如下:
    1. 解析加了@Configuration的配置类
    1. 解析@ComponentScan扫描的包
    1. 解析@ComponentScans扫描的包
    1. 解析@Import 注解 */ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor, PriorityOrdered, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware {

/**

  • Derive further bean definitions from the configuration classes in the registry. */ @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { int registryId = System.identityHashCode(registry); if (this.registriesPostProcessed.contains(registryId)) {

    1. throw new IllegalStateException(
    2. "postProcessBeanDefinitionRegistry already called on this post-processor against " + registry);

    } if (this.factoriesPostProcessed.contains(registryId)) {

    1. throw new IllegalStateException(
    2. "postProcessBeanFactory already called on this post-processor against " + registry);

    } this.registriesPostProcessed.add(registryId);

    processConfigBeanDefinitions(registry); } } ```

    二、ConfigurationClassPostProcessor 类详解

    ```java /**

    • 此类是一个后置处理器,主要功能是参与BeanFactory的建造,主要功能如下:
      1. 解析加了@Configuration的配置类
      1. 解析@ComponentScan扫描的包
      1. 解析@ComponentScans扫描的包
      1. 解析@Import 注解 */ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor, PriorityOrdered, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware {

      /**

      • Derive further bean definitions from the configuration classes in the registry. */ @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { int registryId = System.identityHashCode(registry); if (this.registriesPostProcessed.contains(registryId)) { throw new IllegalStateException( “postProcessBeanDefinitionRegistry already called on this post-processor against “ + registry); } if (this.factoriesPostProcessed.contains(registryId)) { throw new IllegalStateException( “postProcessBeanFactory already called on this post-processor against “ + registry); } this.registriesPostProcessed.add(registryId);

      processConfigBeanDefinitions(registry); }

      /**

      • Prepare the Configuration classes for servicing bean requests at runtime
      • by replacing them with CGLIB-enhanced subclasses. */ @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { int factoryId = System.identityHashCode(beanFactory); if (this.factoriesPostProcessed.contains(factoryId)) { throw new IllegalStateException( “postProcessBeanFactory already called on this post-processor against “ + beanFactory); } this.factoriesPostProcessed.add(factoryId); if (!this.registriesPostProcessed.contains(factoryId)) { // BeanDefinitionRegistryPostProcessor hook apparently not supported… // Simply call processConfigurationClasses lazily at this point then. processConfigBeanDefinitions((BeanDefinitionRegistry) beanFactory); }

      enhanceConfigurationClasses(beanFactory); beanFactory.addBeanPostProcessor(new ImportAwareBeanPostProcessor(beanFactory)); }

      /**

      • Build and validate a configuration model based on the registry of
      • {@link Configuration} classes. */ public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) { // 创建存放BenDefinitionHolder 的对象集合 BeanDefinitionHolder 主要是对BeanDefinition对象对包装 List configCandidates = new ArrayList<>(); // 当前registry就是DefaultListableBeanFactory. 获取所有已经注册对BeanDefinition的beanName String[] candidateNames = registry.getBeanDefinitionNames();

      // 遍历所有需要处理的beanDefinition的名称, 进行筛选被注解修饰的beanDefinition for (String beanName : candidateNames) { BeanDefinition beanDef = registry.getBeanDefinition(beanName); if (ConfigurationClassUtils.isFullConfigurationClass(beanDef) || ConfigurationClassUtils.isLiteConfigurationClass(beanDef)) { if (logger.isDebugEnabled()) {

      1. logger.debug("Bean definition has already been processed as a configuration class: " + beanDef);

      } } else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) { configCandidates.add(new BeanDefinitionHolder(beanDef, beanName)); } }

      // Return immediately if no @Configuration classes were found if (configCandidates.isEmpty()) { return; }

      // Sort by previously determined @Order value, if applicable configCandidates.sort((bd1, bd2) -> { int i1 = ConfigurationClassUtils.getOrder(bd1.getBeanDefinition()); int i2 = ConfigurationClassUtils.getOrder(bd2.getBeanDefinition()); return Integer.compare(i1, i2); });

      // Detect any custom bean name generation strategy supplied through the enclosing application context SingletonBeanRegistry sbr = null; if (registry instanceof SingletonBeanRegistry) { sbr = (SingletonBeanRegistry) registry; if (!this.localBeanNameGeneratorSet) { BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR); if (generator != null) {

      1. this.componentScanBeanNameGenerator = generator;
      2. this.importBeanNameGenerator = generator;

      } } }

      if (this.environment == null) { this.environment = new StandardEnvironment(); }

      // Parse each @Configuration class ConfigurationClassParser parser = new ConfigurationClassParser( this.metadataReaderFactory, this.problemReporter, this.environment, this.resourceLoader, this.componentScanBeanNameGenerator, registry);

      Set candidates = new LinkedHashSet<>(configCandidates); Set alreadyParsed = new HashSet<>(configCandidates.size()); do { parser.parse(candidates); parser.validate();

      Set configClasses = new LinkedHashSet<>(parser.getConfigurationClasses()); configClasses.removeAll(alreadyParsed);

      // Read the model and create bean definitions based on its content if (this.reader == null) { this.reader = new ConfigurationClassBeanDefinitionReader(

      1. registry, this.sourceExtractor, this.resourceLoader, this.environment,
      2. this.importBeanNameGenerator, parser.getImportRegistry());

      } this.reader.loadBeanDefinitions(configClasses); alreadyParsed.addAll(configClasses);

      candidates.clear(); if (registry.getBeanDefinitionCount() > candidateNames.length) { String[] newCandidateNames = registry.getBeanDefinitionNames(); Set oldCandidateNames = new HashSet<>(Arrays.asList(candidateNames)); Set alreadyParsedClasses = new HashSet<>(); for (ConfigurationClass configurationClass : alreadyParsed) {

      1. alreadyParsedClasses.add(configurationClass.getMetadata().getClassName());

      } for (String candidateName : newCandidateNames) {

      1. if (!oldCandidateNames.contains(candidateName)) {
      2. BeanDefinition bd = registry.getBeanDefinition(candidateName);
      3. if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) &&
      4. !alreadyParsedClasses.contains(bd.getBeanClassName())) {
      5. candidates.add(new BeanDefinitionHolder(bd, candidateName));
      6. }
      7. }

      } candidateNames = newCandidateNames; } } while (!candidates.isEmpty());

      // Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes if (sbr != null && !sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) { sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry()); }

      if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) { // Clear cache in externally provided MetadataReaderFactory; this is a no-op // for a shared cache since it’ll be cleared by the ApplicationContext. ((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache(); } }

      /**

      • Post-processes a BeanFactory in search of Configuration class BeanDefinitions;
      • any candidates are then enhanced by a {@link ConfigurationClassEnhancer}.
      • Candidate status is determined by BeanDefinition attribute metadata.
      • @see ConfigurationClassEnhancer */ public void enhanceConfigurationClasses(ConfigurableListableBeanFactory beanFactory) { Map configBeanDefs = new LinkedHashMap<>(); for (String beanName : beanFactory.getBeanDefinitionNames()) { BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName); if (ConfigurationClassUtils.isFullConfigurationClass(beanDef)) { if (!(beanDef instanceof AbstractBeanDefinition)) {
        1. throw new BeanDefinitionStoreException("Cannot enhance @Configuration bean definition '" +
        2. beanName + "' since it is not stored in an AbstractBeanDefinition subclass");
        } else if (logger.isWarnEnabled() && beanFactory.containsSingleton(beanName)) {
        1. logger.warn("Cannot enhance @Configuration bean definition '" + beanName +
        2. "' since its singleton instance has been created too early. The typical cause " +
        3. "is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor " +
        4. "return type: Consider declaring such methods as 'static'.");
        } configBeanDefs.put(beanName, (AbstractBeanDefinition) beanDef); } } if (configBeanDefs.isEmpty()) { // nothing to enhance -> return immediately return; }

      ConfigurationClassEnhancer enhancer = new ConfigurationClassEnhancer(); for (Map.Entry entry : configBeanDefs.entrySet()) { AbstractBeanDefinition beanDef = entry.getValue(); // If a @Configuration class gets proxied, always proxy the target class beanDef.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE); try { // Set enhanced subclass of the user-specified bean class Class<?> configClass = beanDef.resolveBeanClass(this.beanClassLoader); if (configClass != null) {

      1. Class<?> enhancedClass = enhancer.enhance(configClass, this.beanClassLoader);
      2. if (configClass != enhancedClass) {
      3. if (logger.isDebugEnabled()) {
      4. logger.debug(String.format("Replacing bean definition '%s' existing class '%s' with " +
      5. "enhanced class '%s'", entry.getKey(), configClass.getName(), enhancedClass.getName()));
      6. }
      7. beanDef.setBeanClass(enhancedClass);
      8. }

      } } catch (Throwable ex) { throw new IllegalStateException(“Cannot load configuration class: “ + beanDef.getBeanClassName(), ex); } } }

  1. private static class ImportAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
  2. private final BeanFactory beanFactory;
  3. public ImportAwareBeanPostProcessor(BeanFactory beanFactory) {
  4. this.beanFactory = beanFactory;
  5. }
  6. @Override
  7. public PropertyValues postProcessPropertyValues(
  8. PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) {
  9. // Inject the BeanFactory before AutowiredAnnotationBeanPostProcessor's
  10. // postProcessPropertyValues method attempts to autowire other configuration beans.
  11. if (bean instanceof EnhancedConfiguration) {
  12. ((EnhancedConfiguration) bean).setBeanFactory(this.beanFactory);
  13. }
  14. return pvs;
  15. }
  16. @Override
  17. public Object postProcessBeforeInitialization(Object bean, String beanName) {
  18. if (bean instanceof ImportAware) {
  19. ImportRegistry ir = this.beanFactory.getBean(IMPORT_REGISTRY_BEAN_NAME, ImportRegistry.class);
  20. AnnotationMetadata importingClass = ir.getImportingClassFor(bean.getClass().getSuperclass().getName());
  21. if (importingClass != null) {
  22. ((ImportAware) bean).setImportMetadata(importingClass);
  23. }
  24. }
  25. return bean;
  26. }
  27. }
  28. }
  1. <a name="B3TXh"></a>
  2. ## 三、ConfingurationClassPostProcessor 执行流程
  3. - **PostProcessorBeanDefinitionRegistrationDelegate类 调用执行PostProcessors方法,**_**invokeBeanDefinitionRegistryPostProcessors**_
  4. - **BeanDefinitionRegistryPostProcessor 类调用postProcessBeanDefinitionRegistry()方法,这里主要根据具体的实现类去执行子类方法**
  5. - ConfigurationClassPostProcessor. postProcessBeanDefinitionRegistry()
  6. - ConfigurationClassPostProcessor. processConfigBeanDefinitions(registry)
  7. - �**ConfigurationClassPostProcessor类中的processConfigBeanDefinitions()方法核心执行**
  8. - 获取容器中的beanName
  9. - 根据beanNanme 获取得到BeanDefinition
  10. - 判断BeanDefinition 是不是configurationClass, attribute 如果是就跳过操作,不是执行checkConfigurationClassCandidate() 方法
  11. - checkConfigurationClassCandidate() 方法执行流程
  12. - 获取beanDefiniton 对象的元数据 Metadata
  13. - 判断每个Metadata 上只有有注解
  14. - _candidateIndicators_.add(Component.class.getName());<br />_candidateIndicators_.add(ComponentScan.class.getName());<br />_candidateIndicators_.add(Import.class.getName());<br />_candidateIndicators_.add(ImportResource.class.getName());
  15. - @Bean
  16. - 根据第三步的各种判断处理,得到一个需要处理的BeanDefinitionHolder集合对象configCandidates
  17. - 根据集合中对象中order 注解值进排序元素处理顺序
  18. - 获取BeanName 生成策略(beanNameGenerator),即 BeanName的如何生成,没有获取到就使用默认的驼峰命名法。
  19. - 创建ConfigurationClassParser 对象,进行解析每个Configuration 类
  20. - 解析configCandidates 集合对象 parser.parse(candidates)
  21. - for 循环处理每个BeanDefiniiton 对象,判断BeanDefinition 对象是什么类型的
  22. - 根据类型传入不同的参数进行执行 parse() 方法
  23. - processConfigurationClass()进入到解析阶段
  24. - 获取到类资源信息,SourcClass
  25. - 开始真正的执行解析ConfigurationClass类,doProcessConfigurationClass()
  26. - doProcessConfigurationClass(), Spring 中核心的方法,do.....
  27. - 解析执行各种注解
  28. - Process any @PropertySource annotations
  29. - Process any @ComponentScan annotations
  30. - Process any @Import annotations
  31. - Process any @ImportResource annotations
  32. - Process individual @Bean methods
  33. - <br />
  34. <br />代码流程:
  35. ```java
  36. private static void invokeBeanDefinitionRegistryPostProcessors(
  37. Collection<? extends BeanDefinitionRegistryPostProcessor> postProcessors, BeanDefinitionRegistry registry) {
  38. for (BeanDefinitionRegistryPostProcessor postProcessor : postProcessors) {
  39. postProcessor.postProcessBeanDefinitionRegistry(registry);
  40. }
  41. }

四、SpringBoot 中注册Bean对象的基本流程

invokeBeanFactroyPostProcessor.png