spring2.0
中支持
XML Schema
同时继续支持
dtd
,这样在
xml
配置文件中可以使用
dtd
和
schema
两种方式进行声明,示例如下:
spring2.0
及之前版本均支持
dtd
声明:
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
//这里是bean的配置
</ beans >
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
//这里是bean的配置
</ beans >
spring2.0 及以后版本支持 schema 声明:
<?
xml version="1.0" encoding="UTF-8"
?>
<? 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
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
//这里是bean的配置
</ beans >
<? 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
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
//这里是bean的配置
</ beans >
spring2.0 可以新增 <util> 标签进行扩充,使用 <util:list> 、 <util : map> 、 <util:set> 、 <util:properties> 等标签可以取代并简化集合的配置,下面就 4 种标签分别举例说明:
在使用
<util>
标签之前首先要给
xml
配置文件中加入
util
的命名空间,增加后的
spring
配置文件头如下:
<?
xml version="1.0" encoding="UTF-8"
?>
<? xml version="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-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd " >
……
</ beans >
<? xml version="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-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd " >
……
</ beans >
给上面的各种集合配置修改成 <util > 如下:
1
、
list
配置:
<!--
配置一个人物角色
-->
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" >
< list >
< ref bean ="medicine" />
< ref bean ="medicine1" />
</ list >
</ property >
</ bean >
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" >
< list >
< ref bean ="medicine" />
< ref bean ="medicine1" />
</ list >
</ property >
</ bean >
修改成 <util:list> 配置:
<!--
配置一个人物角色
-->
< util:list id ="medicinelist" >
< ref bean ="medicine" />
< ref bean ="medicine1" />
</ util:list >
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" ref ="medicinelist" >
< util:list id ="medicinelist" >
< ref bean ="medicine" />
< ref bean ="medicine1" />
</ util:list >
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" ref ="medicinelist" >
<util:list> 标签可以使用 list-class 来指定的 list 作为使用的集合对象:
<util:list id="medicinelist" list-class="java.util.ArrayList">
2
、
set
配置
<!--
配置一个人物角色
-->
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" >
< set >
< ref bean ="medicine" />
< ref bean ="medicine1" />
</ set >
</ property >
</ bean >
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" >
< set >
< ref bean ="medicine" />
< ref bean ="medicine1" />
</ set >
</ property >
</ bean >
修改成
<util:set>
配置:
<!--
配置一个人物角色
-->
< util:set id ="medicineset" >
< ref bean ="medicine" />
< ref bean ="medicine1" />
</ util:set >
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" ref ="medicineset" />
</ bean >
< util:set >
< util:set id ="medicineset" >
< ref bean ="medicine" />
< ref bean ="medicine1" />
</ util:set >
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" ref ="medicineset" />
</ bean >
< util:set >
标签也可以使用 set-class 来指定使用的集合容器对象:
<util:set id=
"medicineset" set-class="java.util.TreeSet"
>
3
、
map
配置
:
<!--
配置一个人物角色
-->
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" >
< map >
< entry key ="key1" >
< value > 小药丸 </ value >
</ entry >
< entry key ="key2" >
< value > 大药丸 </ value >
</ entry >
</ map >
</ property >
</ bean >
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" >
< map >
< entry key ="key1" >
< value > 小药丸 </ value >
</ entry >
< entry key ="key2" >
< value > 大药丸 </ value >
</ entry >
</ map >
</ property >
</ bean >
修改成 <util:map> 配置:
<!--
配置一个人物角色
-->
< util:map id ="medicinemap" >
< entry key ="key1" value ="小药丸" />
< entry key ="key2" value ="大药丸" />
</ util:map >
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" ref ="medicinemap" />
</ bean >
< util:map id ="medicinemap" >
< entry key ="key1" value ="小药丸" />
< entry key ="key2" value ="大药丸" />
</ util:map >
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" ref ="medicinemap" />
</ bean >
可以使用 map-class 来指定使用的集合对象
4
、
properties
配置:
<!--
配置一个人物角色
-->
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" >
< props >
< propkey ="key1" > 小药丸 </ prop >
< propkey ="key1" > 大药丸 </ prop >
</ props >
</ property >
</ bean >
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" >
< props >
< propkey ="key1" > 小药丸 </ prop >
< propkey ="key1" > 大药丸 </ prop >
</ props >
</ property >
</ bean >
修改成 <util:properties> 配置如下:
<!--
配置一个人物角色
-->
< util:properties id ="medicineprops" >
< prop key ="key1" value ="小药丸" />
< prop key ="key1" value ="大药丸" />
</ util:properties >
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" ref ="medicineprops" />
</ bean >
< util:properties id ="medicineprops" >
< prop key ="key1" value ="小药丸" />
< prop key ="key1" value ="大药丸" />
</ util:properties >
< bean id ="role" class ="spring.chapter2.setDemo.Role" >
< property name ="name" value ="Mary" />
< property name ="health" value ="100" />
< property name ="goods" ref ="medicineprops" />
</ bean >
<util:properties> 可以使用 location 标签来载入外部 properties 文件:
<util:properties id=
"medicineprops" location="classpath:config.properties"/
>