在 Spring 配置文件中,可以使用字面值来提供配置的值。如果配置的值包含 XML 特殊符号,那么可以在属性值外添加一个 XML 特殊处理标签 ,作用是让 XML 解析器将标签中的字符串当作普通文本来对待。
XML 中有 5 个特殊字符,有两种方式可以对这些字符进行特别处理。
,来包裹特殊字符。特殊字符 | 转义序列 |
---|---|
< |
< |
> |
> |
& |
& |
" |
" |
' |
' |
注意: Spring 不会忽略标签内容字符串的前后空格。
Spring IoC 容器中定义的 Bean 可以相互引用。
id="author" class="net.deniro.spring4.bean.Author">
<property name="name" value="deniro"/>
id="book" class="net.deniro.spring4.bean.Book">
<property name="author">
<ref bean="author">ref>
property>
属性 | 说明 |
---|---|
bean | 可以引用同一容器或父容器中的 Bean。 |
local | 只能引用同一个配置文件中定义的 Bean,会自动检查合法性 。 |
parent | 引用父容器中的 Bean ,如 ,说明这个 Bean 的父容器是 xxx。 |
父容器配置:
<bean id="author" class="net.deniro.spring4.bean.Author">
<property name="name" value="deniroFather"/>
bean>
子容器配置:
id="author" class="net.deniro.spring4.bean.Author">
<property name="name" value="deniro"/>
id="book" class="net.deniro.spring4.bean.Book">
<property name="author">
<ref parent="author"/>
property>
子容器配置中存在与父容器相同的 bean id(这里是 author),在 book 中,我们使用的是 ,所以它引用的是父容器的 Bean。
ApplicationContext parent = new ClassPathXmlApplicationContext
(new String[]{"parent.xml"});
ApplicationContext context = new ClassPathXmlApplicationContext(new
String[]{"beans.xml"},
parent);////把父容器上下文作为参数传入
如果某个 Bean 只在一个确定的 Bean 中被引用,那么它可以以内部 Bean 的形式被引用:
id="book" class="net.deniro.spring4.bean.Book">
<property name="author">
class="net.deniro.spring4.bean.Author">
<property name="name" value="deniro"/>
property>
内部 Bean 与 Java 的匿名内部类类似,它没有名字,所以只能在声明处注入实例。
注意:内部 Bean 的 id、name 与 scope 属性配置是无效的,scope 属性默认为 prototype,无法更改。
有时候需要注入一个 null 值,那么可以使用
:
<bean id="book2" class="net.deniro.spring4.bean.Book">
<property name="name"><null/>property>
bean>
Spring 支持级联属性。假设我们希望在 book 中直接注入 author 的名字:
id="book3" class="net.deniro.spring4.bean.Book">
<property name="author.name" value="deniro"/>
还需要初始化 Book 类的 author 属性:
private Author author=new Author();
如果没有初始化 author 属性,Spring 将抛出 NullValueInNestedPathException 异常。
只要配置的 Bean 拥有对应于级联属性的类结构,就可以配置任意层级的级联属性。
Spring 为 List、Set、Map 与 Properties 提供了专属的配置标签。
<bean id="book4" class="net.deniro.spring4.bean.Book">
<property name="labels">
<list>
<value>历史value>
<value>传记value>
list>
property>
bean>
为 Book 添加 labels 属性:
/**
* 标签
*/
private List labels=new ArrayList();
public List getLabels() {
return labels;
}
public void setLabels(List labels) {
this.labels = labels;
}
list 属性既可以通过
注入字符串,也可以使用 注入容器中的其他 Bean。
注意: 一个属性类型,如果可以通过字符串的字面值进行配置,那么该类型所对应的数组类型(如:String[]
、int[]
)也可以采用
的方式进行配置。
Set 配置与 List 配置相似:
<bean id="book5" class="net.deniro.spring4.bean.Book">
<property name="labelSet">
<set>
<value>历史value>
<value>传记value>
set>
property>
bean>
labelSet 在 Book 中必须是 Set 类型:
private Set<String> labelSet=new HashSet<String>();
public Set<String> getLabelSet() {
return labelSet;
}
public void setLabelSet(Set<String> labelSet) {
this.labelSet = labelSet;
}
我们为 Book 添加版本说明:
/**
* 版本
*/
private Map versions=new HashMap();
public Map getVersions() {
return versions;
}
public void setVersions(Map versions) {
this.versions = versions;
}
配置:
<bean id="book6" class="net.deniro.spring4.bean.Book">
<property name="versions">
<map>
<entry>
<key><value>1value>key>
<value>2017-1value>
entry>
<entry>
<key><value>2value>key>
<value>2017-2value>
entry>
map>
property>
bean>
如果某个 Map 元素的键与值是对象,那么可以这样配置:
<entry>
<key><ref bean="keyBean"/>key>
<ref bean="valueBean"/>
entry>
Map 元素的键与值可以是任意类型对象,而 Properties 元素的键与值只能是字符串,所以 Properties 元素其实是 Map 元素的特殊情况。
我们为 Author 添加地址属性列:
/**
* 地址
*/
private Properties addresses=new Properties();
public Properties getAddresses() {
return addresses;
}
public void setAddresses(Properties addresses) {
this.addresses = addresses;
}
配置:
<bean id="author1" class="net.deniro.spring4.bean.Author">
<property name="addresses">
<props>
<prop key="home">北京prop>
<prop key="work">杭州prop>
props>
property>
bean>
因为 Properties 元素的键值只能是字符串,所以配置上比 Map 来的简单。
强类型指的是包装类型,如果 POJO 中,存在强类型的集合,Spring 也可以注入:
/**
* 收入
*/
private Map income=new HashMap();
public Map getIncome() {
return income;
}
public void setIncome(Map income) {
this.income = income;
}
配置:
<bean id="author2" class="net.deniro.spring4.bean.Author">
<property name="income">
<map>
<entry>
<key>
<value>第一季度value>
key>
<value>20000.00value>
entry>
<entry>
<key>
<value>第二季度value>
key>
<value>30000.00value>
entry>
map>
property>
bean>
Spring 容器在注入强类型集合时,会判断元素的类型,把相应的配置值转换为对应的数据类型。
Spring 允许子
继承 父
中同名属性的集合元素。
<bean id="fatherBook" class="net.deniro.spring4.bean.Book" abstract="true">
<property name="labels">
<list>
<value>历史value>
<value>地理value>
list>
property>
bean>
<bean id="childBook" class="net.deniro.spring4.bean.Book" parent="fatherBook">
<property name="labels">
<list merge="true">
<value>天文value>
<value>数学value>
list>
property>
bean>
配置中把父 bean 的 abstract 属性设置为 true,表示是一个抽象的 Bean;然后在子 Bean的 parent 属性中指定父 bean 的 id;最后把集合标签的 merge 设置为 true,表示合并。
上面所说的都是在一个 Bean 中配置集合类型的属性。如果需要一个集合类型的 Bean,则需要使用 util 命名空间。首先在 Spring 的配置文件头中引入 util 命名空间:
<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
">
配置 List:
list id="type" list-class="java.util.LinkedList">
<value>人文value>
<value>科技value>
list>
配置 Set:
set id="type2" set-class="java.util.HashSet">
<value>人文value>
<value>科技value>
set>
配置 Map:
<util:map id="config" map-class="java.util.HashMap">
<entry key="账号" value="admin"/>
<entry key="密码" value="123456"/>
util:map>
提示:
* xxx-class 属性为可选项。
*
与
拥有 value-type 属性,可以指定集合中的值类型。
*
拥有 key-type 和 value-type 属性,可以指定 Map 的键或值的类型。