后端——框架——容器框架——spring_core——容器

  容器知识点有三个部分,概念,application context的类结构,常见操作。

1、概念

IOC的概念,引用原著:

  It is a process whereby objects define their dependencies (that is, the other objects they work with) only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean

这段话包含的三个重点要素

When(何时):we creates the beans(在创建bean的过程中)

Process(过程):we define their dependencies(在定义bean的依赖关系)。

Ways(方式):constructor arguments(构造器参数),arguments to a factory method(工厂方法的参数),set properties(set方法方式)。

  我理解这里的依赖关系是指在创建过程中需要的必要对象或属性,或者是某些必备的前置条件。例如我们在创建数据库对象时,你必须首先加载驱动,它是前置条件。你必须提供用户名,密码,服务器IP,端口号等等,这些都是数据库对象的必要属性。

  数据库对象本身还有许多非必要属性,假设对象创建完成之后,希望测试一下连接,此时需要提供测试的SQL语句,以及尝试次数,等待时间,这些依赖不是发生在创建过程中的。当然我们也可以把这些属性都在创建时一次性指定,而在现实基本都是这样操作的,不存在按需配置的说法。

  从理论角度上,可以指定任何依赖,不一定只是对象的必要属性和必备条件。

  从程序角度上,注入只发生在创建过程中,所有的依赖关系都转换为构造器参数,属性,工厂方法参数三种形式。

  IOC container的概念,引用原著:

  The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata

这段话包含三个要素

ApplicationContext interface represents the spring IOC container,它(applicationContext)就是IOC container的抽象。

它的作用:管理,创建,配置bean。

configuration metadata:元配置信息。

2、类结构

父接口:

EnvironmentCapable:该接口提供获取环境的相关信息,常见的就是操作系统和JVM的信息。

listableBeanFactory:该接口提供了一些很实用的方法,例如根据类型查询bean的数量,判断bean是否存在,bean的总数等等。

HierarchicalBeanFactory:该接口提供了bean的层次结构。

messageResource:实现国际化功能的接口

applicationEventPublisher:发布事件,目前没使用过。

ResourcePatternResolver:它的父接口是ResourceLoader,主要功能是加载资源文件。

AnnotationConfig,Groovy,Xml分别对应三种方式,分别是注解,Groovy配置文件,XML配置文件。Groovy略。

FileSystemApplicationContext在文件系统中查找配置文件。使用频率较低。

3、操作

3.1   配置

配置信息有两种方式,xml和注解。此处只介绍xml方式,注解在之后介绍。

  引入schema,指定schema对应的xsd文件,它会下载xsd文件。断网时,会导致引入schema失败,spring配置文件也完全失效。遇到这种情况时,将XSD文件提前下载保存到本地,然后在编译器中配置XSD。在spring相关的jar包中都有对应的xsd文件。

  使用schema中的标签,标签的格式为namespace:tagName,当schema为默认值时,无需指定namespace,例如context:component-scan。默认情况下都是使用beans作为默认schema。

3.2   创建

applicationContext常见的两个实现类,classpath,annotationConfig。

classpath:对应的配置方式为xml

// 多个XML文件,
Application context = 
new ClassPathXmlApplicationContext(“config1.xml”,”config2.xml”);
// 从文件系统中加载,参数为文件路径,或者是File对象
Application fileSystemContext = 
new FileSystemApplicationContext(“文件路径”);
// 一份配置文件,引入其他配置文件,例如在config1.xml中引入
<import resource=”config1_test.xml”>
<import resource=”config1_product.xml”>

  annotationConfig:对应的配置方式为注解形式

// 单个Config配置文件,类上有@Configuration注解
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class)
// 多个类,通常类上有@Configuration注解,也可以是@Component,@Service,
// @Controller,@Repository。
ApplicationContext ctx = new AnnotationConfigApplicationContext(Test.class, Test2.class);

3.3  使用

使用IOC容器,可以抽象为调用applicationContext对象的API。官网中1.2.3小节只介绍了如何获取容器中的bean。

获取过程有两步操作。

第一步:根据id,name等相关的key,在容器中找到bean,如果bean还未创建,则触发bean的创建过程,并将找到的bean返回。

第二步:将bean映射为Java对象,这也是第二个参数的作用。如果不指定,有可能会报类型转换异常。

原文地址:https://www.cnblogs.com/rain144576/p/14758744.html