目录
2、配置Account类(利用setter方法注入依赖数据)
2、配置Account类(利用setter注入依赖集合数据)
一、介绍
定义
它是SpringBoot框架核心IOC的具体实现。组件之间的依赖关系由容器在应用系统运行期来决定,也就是由动态地将某种依赖关系的目标对象实例注入到应用系统中的各个关联的组件之中。
二、注入方式
(一)构造函数注入
1、构建Account类,提供所有属性的构造方法
顾名思义,就是使用类中的构造函数,给成员变量赋值。注意,赋值的操作不是我们自己做的,而是通过配置的方式,让 Spring
框架来为我们注入。具体代码如下:
public class Account {
private String name;
private Integer age;
private Date birthday;
public Account(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
}
2、配置Account类(使用构造方法注入依赖数据)
<!--使用构造函数的方式:给account中的属性赋值
要求:
类中需要提供一个对应参数列表的构造器函数
涉及的标签:
constructor-arg:
属性:
name: 执行参数在构造器中的名称
value:它能赋的值是基本数据类型和 String 类型
ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean
-->
<bean id="now" class="java.util.Date"></bean>
<bean id="account" class="com.ujiuye.pojo.Account">
<constructor-arg name="name" value="王达"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
3、构造函数注入测试方法
@Test
public void testDI(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Account account = (Account) applicationContext.getBean("account");
System.out.println("name:"+account.getName()+" age:"+account.getAge()+"birthday:"+account.getBirthday());
//打印结果: name:王达 age:20 birthday:Thu Feb 04 13:53:45 CST 2021
}
(二)setter注入
1、修改Account类,添加属性的setter方法
public class Account {
private String name;
private Integer age;
private Date birthday;
public Account() {
}
public Account(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
2、配置Account类(利用setter方法注入依赖数据)
<!--使用set方法的方式给属性赋值
涉及的标签: property
属性:
name:找的是类中set方法后面的部分
ref: 给属性赋值是其他bean类型的
value:给属性赋值是基本数据类型和 string 类型的
实际开发当中, 此种方式用的比较多,推荐使用
-->
<bean id="now" class="java.util.Date"></bean>
<bean id="account" class="com.ujiuye.pojo.Account">
<property name="name" value="张三丰"></property>
<property name="age" value="31"></property>
<property name="birthday" ref="now"></property>
</bean>
3、setter注入测试方法
@Test
public void testDI(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Account account = (Account) applicationContext.getBean("account");
System.out.println("name:"+account.getName()+" age:"+account.getAge()+"birthday:"+account.getBirthday());
//测试结果: name:张三丰 age:31 birthday:Thu Feb 04 14:05:19 CST 2021
}
(三)注入集合数据
1、修改Account类,添加集合属性
给类中的集合成员传值,它用的也是
set
方法注入的方式,只不过变量的数据类型都是集合。
我们这里介绍注入数组,
List,Set,Map,Properties
。具体代码如下:
public class Account {
//注入数组,List集合,Set集合,Map集合,Properties集合属性
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties myProps;
public Account() {
}
public String[] getMyStrs() {
return myStrs;
}
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public List<String> getMyList() {
return myList;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public Set<String> getMySet() {
return mySet;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public Map<String, String> getMyMap() {
return myMap;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public Properties getMyProps() {
return myProps;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
}
2、配置Account类(利用setter注入依赖集合数据)
<!--注入集合类型数据:
涉及到标签:
List结构: array list set
Map结构: map entry props prop
-->
<bean id="account" class="com.ujiuye.pojo.Account">
<!--注意:在注入集合数据时,只要是结构相同,标签可以互换-->
<!--注入数组数据-->
<property name="myStrs">
<array>
<value>array-AAA</value>
<value>array-BBB</value>
<value>array-CCC</value>
</array>
</property>
<!--注入List集合数据-->
<property name="myList">
<list>
<value>list-AAA</value>
<value>list-BBB</value>
<value>list-CCC</value>
</list>
</property>
<!--注入Set集合数据-->
<property name="mySet">
<list>
<value>set-AAA</value>
<value>set-BBB</value>
<value>set-CCC</value>
</list>
</property>
<!--注入Map集合-->
<property name="myMap">
<map>
<entry key="map-a" value="AAA"></entry>
<entry key="map-b">
<value>BBB</value>
</entry>
</map>
</property>
<!--注入Properties集合-->
<property name="myProps">
<props>
<prop key="pro-a">AAA</prop>
<prop key="pro-b">BBB</prop>
</props>
</property>
</bean>
3、集合注入测试方法
@Test
public void testDI(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Account account = (Account) applicationContext.getBean("account");
System.out.println("array:"+ Arrays.toString(account.getMyStrs()));
System.out.println("list:"+account.getMyList());
System.out.println("set:"+account.getMySet());
System.out.println("map:"+account.getMyMap());
System.out.println("props:"+account.getMyProps());
/*
测试结果:
array:[array-AAA, array-BBB, array-CCC]
list:[list-AAA, list-BBB, list-CCC]
set:[set-AAA, set-BBB, set-CCC]
map:{map-a=AAA, map-b=BBB}
props:{pro-b=BBB, pro-a=AAA}
*/
}
三、Spring两种依赖注入方式的比较
目前在Spring Framework中,仅使用构造函数和setter注入两种方式。
构造函数注入 | setter注入 |
没有部分注入 | 有部分注入 |
不会覆盖setter属性 | 会覆盖setter属性 |
任意修改都会创建一个新实例 | 任意修改不会创建一个新实例 |
适用于设置很多属性 | 适用于设置少量属性 |
四、作用
- 降低耦合性:通过依赖注入,可以将对象的依赖关系解耦,减少对象之间的直接耦合,从而增强代码的可维护性和可测试性。
- 提高可重用性:通过依赖注入,可以将依赖对象集中管理,使得多个对象可以共享同一个依赖对象,从而提高代码的可重用性。
- 简化开发:通过依赖注入,可以减少对象的创建和初始化工作,使得开发人员可以更加专注于业务逻辑的实现,从而提高开发效率。
- 便于测试:通过依赖注入,可以方便地模拟或替换依赖对象,使得测试工作可以更加容易地进行,从而提高测试的可靠性和覆盖率。
- 便于维护:通过依赖注入,可以降低代码的复杂度,提高代码的可维护性,从而降低维护成本。