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属性中添加如下值:
| 12
 
 | http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsd
 
 | 
详细如下所示:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | <?xml version="1.0" encoding="GBK"?>
 
 <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>来定义Map的key-value对。使用该标签支持如下三个属性。
| 属性 | 描述 | 
| id | 该属性指定定义一个名为id的 Map Bean实例。 | 
| map-class | 该属性指定 Spring使用哪个Map实现类来创建Bean实例。默认使用HashMap作为实现类 | 
| scope | 指定该 Map Bean实例的作用域。 | 
util:properties元素
<util:properties>:该元素用于加载一份资源文件,并根据加载的资源文件创建一个Properties Bean实例。使用该标签可指定如下几个属性
| 属性 | 描述 | 
| id | 该属性指定定义一个名为 id的Properties Bean实例。 | 
| location | 指定资源文件的位置。 | 
| scope | 指定该 Properties Bean实例的作用域 | 
假设有如下的Bean类文件,这份文件需要List、Set、Map等集合属性。
| 12
 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;
 
 
 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的配置文件来简化这种配置。
| 12
 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"?>
 
 <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">
 
 <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 id="chin.age" static-field=
 "java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
 
 <util:properties id="confTest"
 location="classpath:test_zh_CN.properties"/>
 
 
 <util:list id="chin.schools" list-class="java.util.LinkedList">
 
 <value>小学</value>
 <value>中学</value>
 <value>大学</value>
 </util:list>
 
 
 <util:set id="chin.axes" set-class="java.util.HashSet">
 
 <value>字符串</value>
 <bean class="org.crazyit.app.service.impl.SteelAxe"/>
 <ref bean="stoneAxe"/>
 </util:set>
 
 
 <util:map id="chin.scores" map-class="java.util.TreeMap">
 <entry key="数学" value="87"/>
 <entry key="英语" value="89"/>
 <entry key="语文" value="82"/>
 </util:map>
 
 <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
 
 <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 | 用于简化 Spring的Java EE配置的Schema。 | 
| spring-jms.xsd | 用于简化 Spring关于JMS配置的Schema。 | 
| spring-lang.xsd | 用于简化 Spring动态语言配置的Schema | 
| spring-xsd | 用于简化 Spring事务配置的Schema。 |