IoC (Inversion of Control)
什么是控制反转?
在传统的程序开发中,需要调用对象时,通常由调用者来创建 被调用者 的实例,即对象是由调用者主动 new 出来的。
但是在 Spring 框架中创建对象的工作不再由调用者来完成,而是交给 IoC 容器来创建,再推送给调用者,整个流程完成反转,所以是 控制反转
。
如何使用 IoC?
-
在 Maven 项目的 pom.xml 中导入 Spring 框架包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.11.RELEASE</version> </dependency>
-
通过 IoC 创建对象,在 配置文件(XML格式,文件名可自定义) 中添加需要管理的对象。所有的配置文件都应该放在 resources 文件夹中。
-
配置 xml 文件。
固定格式:
<?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-3.2.xsd"> <bean></bean> </beans>
以 bean 标签的形式来配置对象。每个bean标签需要有一个 id 属性来区分不同的bean 标签,需要有一个 class 属性来对应于要创建的对象的类名。
-
id
:对象名,用来区分不同的 bean 标签。 -
class
:对象的模板类。(所有交给 IoC 容器来管理的类必须有无参构造函数。因为 Spring 底层是通过反射机制来创建对象,调用的是无参构造) -
<property></property>
对象的属性通过 property 标签来完成赋值。name
成员变量名。使用 name 属性前,必须要确保 模板类中有 对应属性的 setter 方法。value
成员变量值。(注意:基本数据类型、String 可以直接赋值,但如果是其他引用类型,不能通过 value 赋值,需要使用 ref)ref
将 IoC 中的另一个 bean 的 id 值 赋给当前的成员变量(DI:依赖注入)
比如使用一个 bean 标签来创建 Student 类,配置如下:
<!-- id属性 用来区分不同的bean标签 --> <!-- class属性 对应于要创建的对象的类名 --> <bean id="student1"class="com.theSunAndSnow.entity.Student"></bean>
你甚至可以在 bean 标签中配置实体类的各种实例域的值。
name
属性值 表示 类属性名value
属性值 表示 类属性值。注意:value 是字符串,所以会有数据格式转换问题。比如若类的 id 属性是 long,那么 value 值也不需要写成 “1L”,直接在字符串中放数字即可(“1”)。
<bean id="student1" class="com.theSunAndSnow.entity.Student"> <property name="id" value="1"></property> <property name="name" value="张三"></property> <property name="age" value="20"></property> </bean>
对应的 Java 类 Student :
package com.theSunAndSnow.entity; import lombok.Data; @Data public class Student { private long id; private String name; private int age; }
-
-
在需要使用 配置文件类 的 Java 类中加载配置文件
// 加载配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springIoC.xml"); // 不需要使用 xml 的绝对路径
-
在 Java 代码中从 IoC 中获取对象
在 ApplicationContext 的实例上使用
Object getBean("beanId")
来获得 类对象。注意:IoC 底层是通过无参构造来创建类的。通过 bean 的 id 属性获取bean
Student student1 = (Student) applicationContext.getBean("student1"); // 参数为 bean 标签的 id 属性值
通过运行时类来获取 bean
Student student1 = (Student) applicationContext.getBean(Student.class);
但是很明显,这种方式有一个局限性:配置文件中只能存在一个数据类型为Student的实例。否则会抛出异常。
通过有参构造来创建 bean
首先,你当然要保证即将生成的类中具有 有参构造 方法。
package com.theSunAndSnow.entity; import lombok.Data; @Data public class Student { private long id; private String name; private int age; private Address address; public Student(long id, String name, int age, Address address) { this.id = id; this.name = name; this.age = age; this.address = address; } }
<bean id="student2" class="com.theSunAndSnow.entity.Student"> <constructor-arg name="id" value="3"></constructor-arg> <constructor-arg name="name" value="Jay"></constructor-arg> <constructor-arg name="age" value="40"></constructor-arg> <constructor-arg name="address" ref="address"></constructor-arg> </bean>
注意:constructor-arg 中的 name 属性可以省略,如果省略,则会按照对应 有参构造函数 中参数的顺序进行赋值
<bean id="student2" class="com.theSunAndSnow.entity.Student"> <constructor-arg value="3"></constructor-arg> <constructor-arg value="Jay"></constructor-arg> <constructor-arg value="40"></constructor-arg> <constructor-arg ref="address"></constructor-arg> </bean>
也可以是哟 index 属性来指明参数赋值顺序
<bean id="student2" class="com.theSunAndSnow.entity.Student"> <constructor-arg index="0" value="3"></constructor-arg> <constructor-arg index="1" value="Jay"></constructor-arg> <constructor-arg index="2" value="40"></constructor-arg> <constructor-arg index="3" ref="address"></constructor-arg> </bean>
-
完成效果:
IoC映射集合
若 bean 映射的类中某个属性是一个集合时,又该如何配置 xml 文件呢?
例如 Student 类有一个List
addressList 集合属性,使用如下方法:<bean id="student3" class="com.theSunAndSnow.entity.Student">
<property name="addressList">
<list>
<ref bean="address"></ref>
<ref bean="address2"></ref>
</list>
</property>
</bean>
<bean id="address" class="com.theSunAndSnow.entity.Address">
<property name="id" value="1"></property>
<property name="name" value="解放路"></property>
</bean>
<bean id="address2" class="com.theSunAndSnow.entity.Address">
<property name="id" value="2"></property>
<property name="name" value="高新区"></property>
</bean>
e=“解放路”>