spring util命名空间

在spring的配置文件中util命名空间类似于java.util包类对应,util命名空间提供了集合相关的配置,在使用命名空间前要导入util命名空间,如下:

  • util命名空间引入
1 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2     xmlns:util="http://www.springframework.org/schema/util"
3     xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
  • spring中配置一个List
1     <!-- 使用util命名空间配置一个List集合 -->
2
<util:list id="userList" value-type="java.lang.String"> 4 <value>张三</value> 5 <value>李四</value> 6 <value>王五</value> 7 </util:list>
  • spring中配置一个Map集合
 1 <!-- 配置一个Map集合 -->
 2     <util:map id="userMap">
 3         <entry key="user1" value-ref="user" />
 4         <entry key="user2">
 5             <!-- 配置一个内部Bean -->
 6             <bean class="io.shuqi.ssh.spring.util.User">
 7                 <property name="userAge" value="12" />
 8                 <property name="userName" value="小张" />
 9             </bean>
10         </entry>
11     </util:map>
  • spring中配置一个Set集合
1 <!-- util配置一个Set集合 -->
2     <util:set id="userSet">
3         <value>张三</value>
4         <value>王五</value>
5         <value>赵六</value>
6     </util:set>

 

  • spring中配置一个Properties集合
<!--配置一个 Properties-->
<util:properties id="userProperties">
        <prop key="name">张三</prop>
        <prop key="age">12</prop>
</util:properties>

<!-- 通过一个properties文件来配置一个properties-->
<util:properties location="classpath:io/shuqi/ssh/spring/util/jdbc.properties" id="jdbc" />

 

实际上<util:list>、<util:map>、<util:set>、<util:properties>等标签是spring用来取代ListFactoryBean、MapFactoryBean、SetFactoryBean、PropertiesFactoryBean的简单写法

  • spring中其他加载配置文件Properties的方式
1     <!-- 
2         context:property-placeholder (PropertySourcesPlaceholderConfigurer)
3             加载一个 properties配置文件,并且可以在其他地方使用${key}表达式来占位属性值,
4             *spring 2.5以前可以注册bean(org.springframework.context.support.PropertySourcesPlaceholderConfigurer)来加载配置文件
5     -->
6     <!-- spring 2.5 以前使用注册bean的方式来加载配置文件 -->
7     <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer" p:location="classpath:io/shuqi/ssh/spring/util/jdbc.properties"></bean>
8     <!-- 使用context命名空间的方式加载Properties文件 -->
9     <context:property-placeholder location="classpath:io/shuqi/ssh/spring/util/jdbc.properties" />

 

posted @ 2015-06-23 15:46  树琦  阅读(4868)  评论(0编辑  收藏  举报