在这一节我们将学习更多spring配置文件中的的标签以及属性的使用。
DI 解决的问题
依赖注入解决的是在bean对象创建时,对象中的参数的初始化问题。我们知道类中包含了属性和方法,在一般的程序中我们在new的时候 提供参数来进行类属性的初始化,而在spring中类的创建的权利交给spring框架,那么当我们使用的类需要使用参数进行初始化的时候要怎么办呢?我们只能通过在配置文件中配置来进行解决,这就是本节学习的有关依赖注入的标签和属性所要解决的问题
依赖注入的方法
- 构造函数注入
- set方法注入
- 通过注解提供(后面学习)
注入的数据类型
- 基本类型和String类型
- 其他bean类型(在配置文件中或注解中配置过的bean)
- 复杂类型(也可以说是集合类型,如list,set,map)
构造函数注入
使用constructor-arg标签:
标签出现的位置:bean标签的内部
标签拥有的属性:
- type 参数的类型,如java.lang.String,java.lang.Integer
- index 参数在构造杉函数中出现的位置,如0,1,2
- name 参数在构造函数中的名称,(用这个来定位是最方便的)
=============以上三个用于指定给构造函数中的那个标签赋值======== - value 基本类型的参数的值
- ref 复杂类型的bean对象的id值
public class AccountService implements IAccountService {
private String name;
private int age;
private Date birthday;
public AccountService(String name, int age, Date birthday){
this.name = name;
this.age = age;
this.birthday = birthday;
}
public void saveAccount() {
System.out.println("AccountService: saveAccount");
}
}<?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 = "now" class="java.util.Date"> </bean>
<bean id = "accountService" class="com.lujuan.service.impl.AccountService" scope="singleton">
<constructor-arg name="name" value="lujuan"> </constructor-arg>
<constructor-arg name="age" value="18"> </constructor-arg>
<constructor-arg name="birthday" ref="now"> </constructor-arg>
</bean>
</beans>set方法注入
在这个方法注入中需要类中有set方法,然后在bean标签内部使用property标签。
使用property标签:
标签出现的位置:bean标签内部
标签拥有的属性:
-name 类中的setXXX函数中的XXX,同时会将第一个大写字母进行小写
-value 基本类型的参数的值
-ref 复杂类型的bean对象的id值
public class AccountService implements IAccountService {
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
private String name;
private int age;
private Date birthday;
}<?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 = "now" class="java.util.Date"> </bean>
<bean id = "accountService" class="com.lujuan.service.impl.AccountService" scope="singleton">
<!--<constructor-arg name="name" value="lujuan"> </constructor-arg>-->
<!--<constructor-arg name="age" value="18"> </constructor-arg>-->
<!--<constructor-arg name="birthday" ref="now"> </constructor-arg>-->
<property name="name" value="lujuan"> </property>
<property name="age" value="18"> </property>
<property name="birthday" ref="now"> </property>
</bean>
基于此,我们已经属性了两种依赖注入的方式。在上面的注入中我们注入的都是基本类型或者bean对象,那么如何注入类似,array、list set map properties这些复杂类型呢,我们接着往下面看。
复杂类型/集合类型注入
集合类型的注入需要使用新的诸如list、set、map、array、props这样的新的标签,并且要放置在constructor-arg标签或者property标签的内部,constructor-arg和property标签仅保留name属性即可,同时对于list、set、array由于结构类似,三种类型可以互换,map和props结构类似可以互换。废话不多说我们直接可以看代码。
public class AccountService implements IAccountService {
public AccountService(String[] myStrs, List<String> myList, Set<String> mySet, Map<String, String> myMap, Properties myPro) {
this.myStrs = myStrs;
this.myList = myList;
this.mySet = mySet;
this.myMap = myMap;
this.myPro = myPro;
}
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String, String> myMap;
private Properties myPro;
public void saveAccount() {
System.out.println("AccountService: saveAccount");
}
}<?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 = "now" class="java.util.Date"> </bean>
<bean id = "accountService" class="com.lujuan.service.impl.AccountService" scope="singleton">
<constructor-arg name="myStrs">
<array>
<value> AAA </value>
</array>
</constructor-arg>
<constructor-arg name="myList">
<list>
<value>AAA</value>
</list>
</constructor-arg>
<constructor-arg>
<set>
<value>AAA</value>
</set>
</constructor-arg>
<constructor-arg name="myMap">
<map>
<entry key="AAA" value="AAAA"> </entry>
</map>
</constructor-arg>
<constructor-arg name="myPro">
<props>
<prop key="AAA"> </prop>
</props>
</constructor-arg>
</bean>
</beans>对于set方法类型的注入使用和上面相同的方法,只不过要把constructor-arg标签换成property标签。
当然我们上面只是用了array、set、list中的value属性以及map、props中的entry、prop属性,还有一下其他属性,比如ref用来引用bean对象,这些我们可以作为后期扩展知识进行学习,本期主要学习的是依赖注入的概念。



京公网安备 11010502036488号