首页 > 其他分享 >理解 Spring IoC 容器

理解 Spring IoC 容器

时间:2022-09-07 16:58:07浏览次数:55  
标签:容器 ConcurrentHashMap Spring getBean Bean 创建 IoC

控制反转与大家熟知的依赖注入同理, 这是通过依赖注入对象的过程. 创建 Bean 后, 依赖的对象由控制反转容器通过构造参数 工厂方法参数或者属性注入. 创建过程相对于普通创建对象的过程是反向, 称之为控制反转 (IoC).

Tomcat 也是 IoC 的一个 Bean

ApplicationContext

ApplicationContext 是 Spring IoC 容器实现的代表, 它负责实例化, 配置和组装 Bean.

IoC 使用元数据配置这种形式, 这个配置元数据表示了应用开发人员告诉 Spring 容器以何种方式实例化 配置和组装应用程序中的对象.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="..."> (1) (2)
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

除了 id 和 class 外, <bean></bean> 标签支持指定 factory-bean factory-method 标识容器以何种方式实例化对象.

元数据

元数据 (BeanDefinition), 一种类似于 java.lang.Class 的定义格式.

  1. SpringApplication.run
  2. AbstractApplicationContext.refresh
  3. AbstractRefreshableApplicationContext.refreshBeanFactory
  4. BeanDefinitionRegistry.registerBeanDefinition (多为 ConcurrentHashMap)

容器对通过 xml 或是其他方式定义的解析成 BeanDefinition 对象, 并保存在 BeanDefinitionRegistry.beanDefinitionMap(ConcurrentHashMap) 中.

BeanDefinition 包括 factory-bean 和 factory-method 以备创建实例.

注意: 如果没有指定构造器这种方式, 会选择无参构造器去创建. 事实上, 大部分构造方式都是无参构造创建, 再由DI setter 注入. 如: Controller Service

Bean 和 getBean

容器没有直接提供一个方法去 set 对象到容器里, 对外只暴漏了 getBean 这个方法. 容器中如果没有 Bean 实例, 这时才会去创建实例.

为了便于理解, 我会分开说.

  • getBean 获取
  1. AbstractBeanFactory.getBean
  2. AbstractBeanFactory.doGetBean
  3. SingletonBeanRegistry.getSingleton (多为 ConcurrentHashMap)
  • createBean 创建
  1. AbstractBeanFactory.getBean
  2. AutowireCapableBeanFactory.createBean
  3. AutowireCapableBeanFactory.doCreateBean
  4. BeanDefinitionRegistry.getBeanDefinition (多为 ConcurrentHashMap)
  5. instantiate (getDeclaredConstructor)
  6. SingletonBeanRegistry.addSingleton (多为 ConcurrentHashMap)

容器将创建好的 Bean addSingleton 到 SingletonBeanRegistry.singletonObjects(ConcurrentHashMap) 中. 需要时, getSingleton 可以直接获取.

Bean 整体加载过程如上, 可以看出在 loadClass 没有被重载的情况下, 走的还是 JVM 的双亲委派.

扫描与注解

之前提到过除了 xml 还可以使用其他方式, 比较常见的就是注解的方式, 这种方式一般搭配扫描注解一起使用. 其实本质上还是找到类的 Class 并且 loadBeanDefinitions 供 instantiate 方法来使用.

标签:容器,ConcurrentHashMap,Spring,getBean,Bean,创建,IoC
From: https://www.cnblogs.com/mossxzzom/p/16666395.html

相关文章

  • springboot通过注解Resource引用指定配置
    yaml配置文件中增加两个不同环境的配置:java配置文件,参考微信支付的代码:/***@author<ahref="https://github.com/binarywang">BinaryWang</a>*/@Slf4j@Config......
  • springboot的日志配置
    转载:https://blog.csdn.net/tz845195485/article/details/123361895#========================logging日志相关的配置=====================#日志级别trace<debug<inf......
  • IOC入门
    2.3.2IOC、IOC容器、Bean、DI1.IOC(InversionofControl)控制反转(1)什么是控制反转呢?使用对象时,由主动new产生对象转换为由外部提供对象,此过程中对象创建控制权由程序......
  • SpringBoot解决BigDecimal传到前端后精度丢失问题
    1、局部处理(1)在相应字段上加@JsonFormat@JsonFormat(shape=JsonFormat.Shape.STRING)(2)在相应字段上加@JsonSerialize@JsonSerialize(using=ToStringSerializer.class......
  • Spring学习笔记(二)
    Spring配置文件引入外部属性引入数据库和Druid的依赖在配置文件中配置DataSource的bean 直接配置属性<beanid="dataSource"class="com.alibaba.druid.pool.DruidDa......
  • spring-boot spring-cloud spring-cloud-alibaba版本对应
    https://spring.io/projects/spring-cloud#overview   https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E ......
  • springboot集成hibernate-validator
    一、项目搭建1、使用springboot搭建一个web工程建web工程,不使用骨架创建maven的Java工程即可,不需要创建maven的web工程。2、添加父工程坐标和添加web启动器<parent>......
  • 跟着黑马学SSM——Day5之Spring事务
    Spring事务简介事务作用:在数据层保障一系列的数据库操作同成功同失败Spring事务作用:在数据层或业务层一系列的数据库操作同成功同失败案例:银行转账需求:实现任意两......
  • SpringBoot使用自定义注解+AOP+Redis实现接口限流
    为什么要限流系统在设计的时候,我们会有一个系统的预估容量,长时间超过系统能承受的TPS/QPS阈值,系统有可能会被压垮,最终导致整个服务不可用。为了避免这种情况,我们就需要对......
  • SpringBoot常用注解
    SpringBoot常用注解1.@SpringBootApplicationspringBoot的基石,启动类@Configuration应许spring注册额外的bean或者导入其他配置类@EnableAutoConfiguration启用Sp......