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>