运行环境
- jdk1.8
- idea
- maven-3.8.1
- spring-5.3.27
IOC容器
IOC容器是具有依赖注入功能的容器,负责对象的实例化、对象的初始化,对象和对象之间依赖关系配置、对象的销毁、对外提供对象的查找等操作,对象的整个生命周期都是由容器来控制。我们需要使用的对象都由ioc容器进行管理,不需要我们再去手动通过new的方式去创建对象,由ioc容器直接帮我们组装好,当我们需要使用的时候直接从ioc容器中直接获取就可以了。
那么spring ioc容器是如何知道需要管理哪些对象呢?
需要我们给ioc容器提供一个配置清单,这个配置支持xml格式和java注解的方式,在配置文件中列出需要让ioc容器管理的对象,以及可以指定让ioc容器如何构建这些对象,当spring容器启动的时候,就会去加载这个配置文件,然后将这些对象给组装好以供外部访问者使用。
这里所说的IOC容器也叫spring容器。
Bean概念
由spring容器管理的对象统称为Bean对象。Bean就是普通的java对象,和我们自己new的对象其实是一样的,只是这些对象是由spring去创建和管理的,我们需要在配置文件中告诉spring容器需要创建哪些bean对象,所以需要先在配置文件中定义好需要创建的bean对象,这些配置统称为bean定义配置元数据信息,spring容器通过读取这些bean配置元数据信息来构建和组装我们需要的对象。
Spring容器使用步骤
- 引入spring相关的maven配置
- 创建bean配置文件,比如bean xml配置文件
- 在bean xml文件中定义好需要spring容器管理的bean对象
- 创建spring容器,并给容器指定需要装载的bean配置文件,当spring容器启动之后,会加载这些配置文件,然后创建好配置文件中定义好的bean对象,将这些对象放在容器中以供使用
- 通过容器提供的方法获取容器中的对象,然后使用
Spring容器对象
spring内部提供了很多表示spring容器的接口和对象,我们来看看比较常见的几个容器接口和具体的实现类。
BeanFactory接口
org.springframework.beans.factory.BeanFactory
spring容器中具有代表性的容器就是BeanFactory接口,这个是spring容器的顶层接口,提供了容器最基本的功能。
常用的几个方法
1 2 3 4 5 6 7 8 9 10 11
| Object getBean(String name) throws BeansException
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
<T> T getBean(Class<T> requiredType) throws BeansException;
<T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);
|
ApplicationContext接口
org.springframework.context.ApplicationContext
这个接口继承了BeanFactory接口,所以内部包含了BeanFactory所有的功能,并且在其上进行了扩展,增加了很多企业级功能,比如AOP、国际化、事件支持等等。
ClassPathXmlApplicationContext类
org.springframework.context.support.ClassPathXmlApplicationContext
这个类实现了ApplicationContext接口,注意一下这个类名称包含了ClassPath Xml,说明这个容器类可以从classpath中加载bean xml配置文件,然后创建xml中配置的bean对象,一会后面的案例就会用到这个类。
AnnotationConfigApplicationContext类
org.springframework.context.annotation.AnnotationConfigApplicationContext
这个类也实现了ApplicationContext接口,注意其类名包含了Annotation和config两个单词,上面我们有说过,bean的定义支持xml的方式和注解的方式,当我们使用注解的方式定义bean的时候,就需要用到这个容器来装载了,这个容器内部会解析注解来构建构建和管理需要的bean。
注解的方式相对于xml方式更方便一些,也是我们比较推荐的方式,后面我们会大量使用这种方式,具体会详解。
案例
使用spring输出helloworld
创建项目spring-series
使用idea创建maven项目spring-series,项目坐标:
1 2 3
| <groupId>org.iocbean2023</groupId> <artifactId>spring-series</artifactId> <version>1.0-SNAPSHOT</version>
|
目前我们使用spring的版本5.3.27,需要引入spring提供的3个构件
spring-core、spring-context、spring-beans
spring-series项目中创建一个子模块lesson-001,项目maven父子结构,如下图:
spring-series/pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>org.iocbean2023</groupId> <artifactId>spring-series</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>lesson-001</module> </modules>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <spring.version>5.3.27</spring.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </dependencyManagement>
</project>
|
lesson-001\pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-series</artifactId> <groupId>org.iocbean2023</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>lesson-001</artifactId>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties>
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> </dependencies>
</project>
|
lesson-001中创建HelloWord类
1 2 3 4 5 6 7 8 9
| package demo;
public class HelloWord {
public void say() { System.out.println("hello,Word!"); } }
|
HelloWord中我们创建了一个say方法,里面会输一段文字。
使用spring容器
下面我们通过spring容器来创建HelloWord对象,并从容器中获取这个对象,然后调用其say方法输出文字。
创建bean xml配置文件
新建一个文件,文件路径如下:
spring-series\lesson-001\src\main\resources\bean.xml
bean.xml内容如下:
1 2 3 4 5 6 7 8
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloWorld" class="demo.HelloWord"/>
</beans>
|
上面就是bean的定义文件,每个xml中可以定义多个bean元素,通过bean元素定义需要spring容器管理的对象,bean元素需指定id和class属性
上面的配置文件中我们定义了一个helloWorld标识的HellWorld类型的bean对象。
创建测试类
创建一个Client类,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package demo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client { public static void main(String[] args) {
String beanXml = "classpath:/bean.xml";
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(beanXml);
HelloWord helloWorld = context.getBean("helloWorld", HelloWord.class);
helloWorld.say();
} }
|
上面main方法中有容器的详细使用步骤,需要先创建容器对象,创建容器的对象的时候需要指定bean xml文件的位置,容器启动之后会加载这些配文件,然后将这些对象构建好。
代码中通过容器提供的getBean方法从容器中获取了HellWorld对象,第一个参数就是xml中bean的id,第二个参数为bean对应的Class对象。
运行输出
最后项目目录结构如下:
总结
本文主要介绍了spring容器的概念、bean的概念、常见的spring容器,以及spring容器的使用步骤;下一篇我们将详解bean的定义。