Spring的util标签的使用

探索<util/>命名空间

首先在spring的配置文件中添加

Xml 代码
  1. <beans
  2. xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:util=<A href="http://www.springframework.org/schema/util">http://www.springframework.org/schema/util</A>
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  8. http://www.springframework.org/schema/util
  9. <A href="http://www.springframework.org/schema/util/spring-util-2.0.xsd">http://www.springframework.org/schema/util/spring-util-2.0.xsd">
  10. </A>
<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-2.0.xsd                     http://www.springframework.org/schema/util                     http://www.springframework.org/schema/util/spring-util-2.0.xsd"> 

1. <util:constant/>元素

比如某类存在如下字段定义

Java代码
  1. public static final String hwStatic = "hello static constant";
public static final String hwStatic = "hello static constant";
如果希望以上属性取值作为受管Bean,可以如下配置: Xml代码
  1. <util:constant id="hwConstant" static-field="test.HelloWorld.hwStatic"/>
<util:constant id="hwConstant" static-field="test.HelloWorld.hwStatic"/>

这样就将java代码中的常量hwStatic(在test包下的HelloWorld类中)配置给spring进行管理,id为另起的名字;

又eg:

Xml代码
  1. <util:constant id="maxValue" static-field="java.lang.Integer.MAX_VALUE"/>
<util:constant id="maxValue" static-field="java.lang.Integer.MAX_VALUE"/>

2. <util:property-path/>元素

Xml代码
  1. id="property-path" path="helloWorld.hello"/>
  2. <bean id="helloWorld" class="test.HelloWorld">
  3. <property name="hello" value="http://zchuan-2006.iteye.com/blog/hi"/>
  4. </bean>
id="property-path" path="helloWorld.hello"/><bean id="helloWorld" class="test.HelloWorld">     <property name="hello" value="http://zchuan-2006.iteye.com/blog/hi"/></bean>

这里path="helloworld.hello"就是指bean为"helloworld"的属性hello。

3. <util:properties/>元素

"classpath:"表明,将从类路径上查找并装载xxx属性文件.

Xml代码
  1. <util:properties id="xxx" location="classpath:xxxxx.properties">
<util:properties id="xxx" location="classpath:xxxxx.properties"> 

4. <util:list/>元素

Xml代码
  1. <util:list id="listUtil" list-class="java.util.ArrayList">
  2. <value>first</valuse>
  3. <value>two</valuse>
  4. <value>three</valuse>
  5. <value>ten</valuse>
  6. </util:list>
<util:list id="listUtil" list-class="java.util.ArrayList">    <value>first</valuse>    <value>two</valuse>    <value>three</valuse>    <value>ten</valuse></util:list>

它的作用就是在spring启动初始化bean时,给listUtil这个list赋值为这四个值。 下面的同理

5. <util:map/>元素

Xml代码
  1. <bean id="abstractCollectionBean" abstract="true">
  2. <property name="map">
  3. <map>
  4. <entry key="mapKey1" value="http://zchuan-2006.iteye.com/blog/mapValue1">
  5. <entry key="mapKey2" value="http://zchuan-2006.iteye.com/blog/mapValue2">
  6. </map>
  7. </property>
  8. </bean>
<bean id="abstractCollectionBean" abstract="true">    <property name="map">        <map>            <entry key="mapKey1" value="http://zchuan-2006.iteye.com/blog/mapValue1">            <entry key="mapKey2" value="http://zchuan-2006.iteye.com/blog/mapValue2">        </map>    </property></bean>
继承了abstractCollectionBean的子bean Xml代码
  1. <bean id="CollectionBean" class="test.CollectionBean" parent="abstractCollectionBean">
  2. <property name="map">
  3. <map merge="true" key-type="java.lang.String" value-type="java.lang.String">
  4. <entry key="mapKey1" value="http://zchuan-2006.iteye.com/blog/mapValue1Override"/>
  5. <entry>
  6. <key><value>mapKey2</value></key>
  7. <value>mapValue2</value>
  8. </entry>
  9. <entry key="testBean" value-ref="testBean">
  10. </map>
  11. </property>
  12. </bean>
  13. <bean id="testBean" class="test.TestBean" />
<bean id="CollectionBean"  class="test.CollectionBean" parent="abstractCollectionBean">    <property name="map">        <map merge="true" key-type="java.lang.String" value-type="java.lang.String">            <entry key="mapKey1" value="http://zchuan-2006.iteye.com/blog/mapValue1Override"/>            <entry>                <key><value>mapKey2</value></key>                <value>mapValue2</value>            </entry>            <entry key="testBean" value-ref="testBean">        </map>    </property></bean><bean id="testBean" class="test.TestBean" />

为了简化MapFactoryBean对象的使用,可使用如下代码 :

Xml代码
  1. <util:map id="mapUtil" map-class="java.util.HashMap">
  2. <entry key="1" value="http://zchuan-2006.iteye.com/blog/first">
  3. <entry key="2" value="http://zchuan-2006.iteye.com/blog/two">
  4. <entry key="3" value="http://zchuan-2006.iteye.com/blog/three">
  5. </util:map>
<util:map id="mapUtil" map-class="java.util.HashMap">    <entry key="1" value="http://zchuan-2006.iteye.com/blog/first">    <entry key="2" value="http://zchuan-2006.iteye.com/blog/two">    <entry key="3" value="http://zchuan-2006.iteye.com/blog/three"></util:map>

6. <util:set/>元素

同样的,为了简化SetFactoryBean对象,可使用如下代码 :

Xml代码
  1. <util:set id="setUtil" set-class="java.util.HashSet">
  2. <value>first</value>
  3. <value>two</value>
  4. <value>three</value>
  5. </util:set>
<util:set id="setUtil" set-class="java.util.HashSet">    <value>first</value>    <value>two</value>    <value>three</value></util:set>

7. 使用<p/>命名空间

在xml头加入 xmlns:p=http://www.springframework.org/schema/p;这里的p就是property的意思。

例如如下代码:

Xml代码
  1. <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  2. <property name="locations" ref="locations"/>
  3. <property name="order" value="http://zchuan-2006.iteye.com/blog/1"/>
  4. </bean>

  5. <util:list id="locations">
  6. <value>userinfo.properties</value>
  7. </util:list>
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="locations" ref="locations"/>    <property name="order" value="http://zchuan-2006.iteye.com/blog/1"/></bean><util:list id="locations">    <value>userinfo.properties</value></util:list>

在导入了</p>命名空间后,等价于

Xml代码
  1. <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:locations-ref="locations" p:order="1" />

  2. <util:list id="locations">
  3. <value>userinfo.properties</value>
  4. </util:list>

事情的发展总是一段曲折前进的过程。当Spring刚出现时,开发者可以使用<list/>、<map/>、<set/>等元素定义集合,然而这些集合不能够在不同的受管Bean间进行复用。尽管开发者可以采用抽象Bean机制实现复用,但实在不怎么优雅。与此同时,开发者借助ListFactoryBean、MapFactoryBean和SetFactoryBean等对象能够定义出可供复用的集合。然而,这也不是很友好的做法。再后来,<util/>命名空间被Spring 2.x引入,这才使得集合的定义变得简单。

你可能感兴趣的:(Spring的util标签的使用)