这篇文章,详细介绍一下Spring框架中如何注入各种数据类型,包含:注入基本数据类型、数组、集合、Map映射、Property属性、注入空字符串、注入null值、注入特殊字符等内容,以及如何使用命名空间进行依赖注入。
目录
注入基本数据类型,既可以采用setter方法注入,也可以采用构造方法注入,两种方式都有不同的限制条件:
这里我以setter方法注入为案例:

以上,就是setter方法注入的用法,只要在Spring的XML配置文件里面采用【
在实际开发过程中,我们一个类里面会用到另外一个类的对象,这个时候就是注入引用对象类型,也就是我们说的Bean对象。注入Bean对象,首先需要确保Bean对象已经被实例化了,然后另外一个Bean里面才能够注入。
注入Bean分为两种:
外部注入Bean方式,是指:A类中使用了B类的对象,那么首先需要将B类对象进行实例化,然后A类直接引用B类实例化之后的对象。
这里举个栗子,员工和部门的关系,我们就可以两个类来表示,一个是【Emp】员工类,另外一个是【Dept】部门类,案例代码如下所示:
创建【Dept】类:
- public class Dept {
- private Integer deptId;
- private String deptName;
-
- public Integer getDeptId() {
- return deptId;
- }
-
- public void setDeptId(Integer deptId) {
- this.deptId = deptId;
- }
-
- public String getDeptName() {
- return deptName;
- }
-
- public void setDeptName(String deptName) {
- this.deptName = deptName;
- }
- }
创建【Emp】类:
- public class Emp {
- private Integer empId;
- private String empName;
- private Dept dept; // 部门类的对象
-
- public Integer getEmpId() {
- return empId;
- }
-
- public void setEmpId(Integer empId) {
- this.empId = empId;
- }
-
- public String getEmpName() {
- return empName;
- }
-
- public void setEmpName(String empName) {
- this.empName = empName;
- }
-
- public Dept getDept() {
- return dept;
- }
-
- public void setDept(Dept dept) {
- this.dept = dept;
- }
- }
添加XML配置

我们把这种直接引用外部Bean的方式,叫做注入外部Bean。
可以发现,我们这里使用的【
外部注入Bean方式是引用外部的一个Bean实例,那内部注入Bean就是不引用外部的Bean呗,而是直接在当前的Bean里面进行实例化。
具体XML配置如下所示:
- <bean id="emp2" class="com.spring.demo.pojo.Emp">
-
- <property name="empId" value="1001"/>
- <property name="empName" value="张三"/>
-
- <property name="dept">
- <bean class="com.spring.demo.pojo.Dept">
-
- <property name="deptId" value="2001"/>
- <property name="deptName" value="研发部"/>
- bean>
- property>
- bean>
是不是很简单,内部注入bean其实就是把另外一个bean的配置移到【
如果类中的某个属性是数组,那就不能和之前一样编写了,因为之前的都是注入单独一个的值,而数组可以有多个值,那么如何注入数组呢???
注入数组,可以借助【

采用【
注入集合和注入数组类似的,只不过注入List集合是采用【】标签进行标识。
- <bean id="collectionType1" class="com.spring.demo.pojo.CollectionType">
-
- <property name="listData">
- <list>
- <value>张三value>
- <value>李四value>
- <value>王五value>
- list>
- property>
- bean>
注入Set集合是采用【
- <bean id="collectionType2" class="com.spring.demo.pojo.CollectionType">
-
- <property name="setData">
- <set>
- <value>张三value>
- <value>李四value>
- <value>王五value>
- set>
- property>
- bean>
注入map比较特殊,因为map和List、Set集合不同,map是具有key-value形式的数据,所以注入map集合,需要采用【
【
具体案例代码如下所示:
- <bean id="collectionType3" class="com.spring.demo.pojo.CollectionType">
-
- <property name="map">
- <map>
- <entry key="1" value="Tom"/>
- <entry key="2" value="Jerry"/>
- <entry key="3" value="Jack"/>
- map>
- property>
- bean>
Property是HashMap的一个子类,它也是满足key-value形式的数据集合,通过【
注入Property属性案例:
- <bean id="collectionType4" class="com.spring.demo.pojo.CollectionType">
-
- <property name="properties">
- <props>
- <prop key="1">Tomprop>
- <prop key="2">Jerryprop>
- <prop key="3">Jackprop>
- props>
- property>
- bean>
如何定义Property文件???
首先需要创建一个以【.properties】为后缀的文件。
然后在文件中,通过【key=value】的格式,编写对应的数据内容。
注意:一行只能编写一个【key=value】。
从Property文件读取属性案例:
- driver=com.jdbc.mysql.Driver
- url=jdbc:mysql://localhost:3306/test
- username=root
- password=root
- public class JdbcProperties {
- private String driver;
- private String url;
- private String username;
- private String password;
-
- // setter and getter
- }
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
-
-
- <bean id="collectionType4" class="com.spring.demo.pojo.CollectionType">
-
- <property name="properties">
- <props>
- <prop key="1">Tomprop>
- <prop key="2">Jerryprop>
- <prop key="3">Jackprop>
- props>
- property>
- bean>
-
-
- <context:property-placeholder location="jdbc.properties" />
-
- <bean id="jdbcProperties" class="com.spring.demo.pojo.JdbcProperties">
-
- <property name="driver" value="${driver}"/>
- <property name="url" value="${url}"/>
- <property name="username" value="${username}"/>
- <property name="password" value="${password}"/>
- bean>
-
- beans>
以上,就是注入Property类型。
如何注入null值呢???通过【
- <bean id="nullAndEmpty" class="com.spring.demo.pojo.NullAndEmpty">
-
- <property name="name">
- <null/>
- property>
- bean>
如何注入空字符串呢???直接设置【value】属性等于空即可。
- <bean id="nullAndEmpty2" class="com.spring.demo.pojo.NullAndEmpty">
-
- <property name="name" value=""/>
- bean>
在XML文件里面,有时候注入的值存在一些特殊字符(例如:<,>符号),此时XML文件会解析失败,因为(<,>符号)是XML文件规定的标签开始结束标记,我们不能在注入的值里面使用???那要如何解决这个问题呢???
解决办法:
举例如下:

以上,就是注入特殊字符的方式。
从第一部分,我们学习了各种数据类型的注入方式,可以看到,每次我们注入一个属性的时候,都需要编写很多的【
这里说的【p命名空间】是用于简写【
添加【p命名空间】前缀
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd">
-
- beans>
p命名空间使用案例
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd">
-
-
- <bean id="user" class="com.spring.demo.pojo.User"
- p:uid="1001"
- p:username="张三"
- p:password="123456"/>
-
- beans>
上面,可以看到我们只需要在【
这里说的【c命名空间】是用于简写【
添加【c命名空间】前缀
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:c="http://www.springframework.org/schema/c"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd">
-
- beans>
c命名空间使用
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:c="http://www.springframework.org/schema/c"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd">
-
-
- <bean id="cnamespace" class="com.spring.demo.pojo.Cnamespace" c:_0="1001" c:_1="李四" />
-
-
- <bean id="cnamespace1" class="com.spring.demo.pojo.Cnamespace" c:id="1001" c:name="李四" />
-
- beans>
【c命名空间】是构造方法注入的简写,可以两种方式:
util命名空间,可以将注入集合的那些部分提取出一个公共内容,然后其他地方需要注入的时候,就可以直接引用,而不需要重复的编写。
添加【util命名空间】前缀
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-4.0.xsd">
-
- beans>
util命名空间的使用

util命名空间,常用的有如下几个:
【
综上,这篇文章,详细介绍一下Spring框架中如何注入各种数据类型,包含:注入基本数据类型、数组、集合、Map映射、Property属性、注入空字符串、注入null值、注入特殊字符等内容,以及如何使用命名空间进行依赖注入。