Spring笔记4---<util/>、<p/>命名空间

1. <util:constant/>元素

 

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

 

 

public static final String hwStatic = "hello static";

public String hw = "hw";

 

如果希望以上属性取值作为受管Bean,可以如下配置:

 

 

<util:constant id="hwConstant" static-field="test.HelloWorld.hwStatic"/>

 

 

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

 

 

<util:property-path id="property-path" path="helloWorld.hello"/>
<bean id="helloWorld" class=''test.HelloWorld">
<property name="hello" value="hi"/>
</bean>

 

 

3. <util:properties/>元素

 

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

 

 

<util:properties id="xxx" location="classpath:xxxxx.properties"> 

 

 

<util:list id="listUtil" list-class="java.util.ArrayList">
<value>first</valuse>
<value>two</valuse>
<value>three</valuse>
<value>ten</valuse>
</util:list>
 

 

4. <util:map/>元素

 

 

 

<bean id="abstractCollectionBean" abstract="true">
<property name="map">
<map>
<entry key="mapKey1" value="mapValue1">
<entry key="mapKey2" value="mapValue2">
</map>
</property>

</bean>

 

 

    继承了abstractCollectionBean的子bean

<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="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对象的使用,可使用如下代码

 

<util:map id="mapUtil" map-class="java.util.HashMap">
<entry key="1" value="first">
<entry key="2" value="two">
<entry key="3" value="three">
</util:map>
 

 

5. <util:set/>元素

 

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

 

 

<util:set id="setUtil" set-class="java.util.HashSet">
<value>first</value>
<value>two</value>
<value>three</value>
</util:set>

 

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

 

    在xml头加入

 

 xmlns:p="http://www.springframework.org/schema/p"

  

 

    例如如下代码:

 

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" ref="locations"/>
<property name="order" value="1"/>

</bean>

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

</util:list>

 

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

 

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


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

</util:list>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值