7.11.3 使用util命名空间简化配置

7.11.3 使用util命名空间简化配置

Spring框架解压缩包的schema\util\路径下包含有util:命名空间的XML Schema文件,为了使用util:命令空间的元素,必须先在Spring配置文件中导入最新的spring-util.xsd,也就是需要在Spring配置配置文件中添加xmlns:util属性该属性值如下:

1
xmlns:util="http://www.springframework.org/schema/util"

,以及在xsi:schemaLocation属性中添加如下值:

1
2
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd

详细如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的根元素和Schema
导入p:命名空间和util:命名空间的元素 -->
<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"
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 Schema下的元素

util Schema下提供了如下几个元素。

util:constant元素

<util:constant>元素用于获取指定类的静态Field的值。它是FieldRetrievingFactory Bean的简化配置。

util:property-path元素

<util:property-path>元素用于获取指定对象的getter方法的返回值。它是PropertyPathFactoryBean的简化配置。

util:list元素

<util:list>元素用于定义一个List bean,支持使用<value><ref><bean>等子元素来定义List集合元素。使用该标签支持如下三个属性。

属性 描述
id 该属性指定定义一个名为id的List bean实例。
list-class 该属性指定Spring使用哪个List实现类来创建Bean实例。默认使用ArrayList作为实现类。
scope 指定该List bean实例的作用域。

set元素

set:该元素用于定义一个Set Bean,支持使用<value><ref><bean>等子元素来定义set集合元素。使用该标签支持如下三个属性。

属性 描述
id 该属性指定定义一个名为id的Set bean实例。
set-class 该属性指定Spring使用哪个Set实现类来创建Bean实例。默认使用HashSet作为实现类。
scope 指定该Set bean实例的作用域。

util:map元素

<util:map>元素用于定义一个Map Bean,支持使用<entry>来定义Mapkey-value对。使用该标签支持如下三个属性。

属性 描述
id 该属性指定定义一个名为id的Map Bean实例。
map-class 该属性指定Spring使用哪个Map实现类来创建Bean实例。默认使用HashMap作为实现类
scope 指定该Map Bean实例的作用域。

util:properties元素

<util:properties>:该元素用于加载一份资源文件,并根据加载的资源文件创建一个Properties Bean实例。使用该标签可指定如下几个属性

属性 描述
id 该属性指定定义一个名为idProperties Bean实例。
location 指定资源文件的位置。
scope 指定该Properties Bean实例的作用域

假设有如下的Bean类文件,这份文件需要ListSetMap等集合属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package org.crazyit.app.service.impl;
import java.util.*;
import org.crazyit.app.service.*;
public class Chinese implements Person
{
private Axe axe;
private int age;
private List schools;
private Map scores;
private Set axes;
// 此处省略getter和setter方法,请自己补上
// 实现Person接口的useAxe()方法
public void useAxe()
{
System.out.println(axe.chop());
System.out.println("age属性值:" + age);
System.out.println(schools);
System.out.println(scores);
System.out.println(axes);
}
}

下面使用基于XML Schema的配置文件来简化这种配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的根元素和Schema
导入p:命名空间和util:命名空间的元素 -->
<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"
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">
<!-- 配置chinese实例,其实现类是Chinese -->
<bean id="chinese" class="org.crazyit.app.service.impl.Chinese"
p:age-ref="chin.age" p:axe-ref="stoneAxe"
p:schools-ref="chin.schools"
p:axes-ref="chin.axes"
p:scores-ref="chin.scores"/>
<!-- 使用util:constant将指定类的静态Field定义成容器中的Bean -->
<util:constant id="chin.age" static-field=
"java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
<!-- 使用util.properties加载指定资源文件 -->
<util:properties id="confTest"
location="classpath:test_zh_CN.properties"/>
<!-- 使用util:list定义一个List集合,指定使用LinkedList作为实现类,
如果不指定默认使用ArrayList作为实现类 -->
<util:list id="chin.schools" list-class="java.util.LinkedList">
<!-- 每个value、ref、bean...配置一个List元素 -->
<value>小学</value>
<value>中学</value>
<value>大学</value>
</util:list>
<!-- 使用util:set定义一个Set集合,指定使用HashSet作为实现类,
如果不指定默认使用HashSet作为实现类-->
<util:set id="chin.axes" set-class="java.util.HashSet">
<!-- 每个value、ref、bean...配置一个Set元素 -->
<value>字符串</value>
<bean class="org.crazyit.app.service.impl.SteelAxe"/>
<ref bean="stoneAxe"/>
</util:set>
<!-- 使用util:map定义一个Map集合,指定使用TreeMap作为实现类,
如果不指定默认使用HashMap作为实现类 -->
<util:map id="chin.scores" map-class="java.util.TreeMap">
<entry key="数学" value="87"/>
<entry key="英语" value="89"/>
<entry key="语文" value="82"/>
</util:map>
<!-- 配置steelAxe实例,其实现类是SteelAxe -->
<bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
<!-- 配置stoneAxe实例,其实现类是StoneAxe -->
<bean id="stoneAxe" class="org.crazyit.app.service.impl.StoneAxe"/>
</beans>

上面的配置文件完整地示范了util Schema下的各简化标签的用法。从上面的配置文件可以看出,使用这种简化标签可让Spring配置文件更加简洁

其他常用简化Schema

除此之外,关于Spring其他常用的简化Schema简要说明如下。

Schema 描述
spring-aop.xsd 用于简化Spring AOP配置的Schema
spring-jee.xsd 用于简化SpringJava EE配置的Schema
spring-jms.xsd 用于简化Spring关于JMS配置的Schema
spring-lang.xsd 用于简化Spring动态语言配置的Schema
spring-xsd 用于简化Spring事务配置的Schema