`

[转] 关于spring中<util:/>的配置

阅读更多

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

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

<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/>元素
比如某类存在如下字段定义  

public static final String hwStatic = "hello static constant"; 


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

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


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

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



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

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


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

3. <util:properties/>元素
    "classpath:"表明,将从类路径上查找并装载xxx属性文件.  

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



4. <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/>元素

<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>  


  
6. <util:set/>元素
   同样的,为了简化SetFactoryBean对象,可使用如下代码 :

<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的意思。
 
     例如如下代码:  

<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>

    
实例:http://blog.csdn.net/daryl715/archive/2007/09/26/1802292.aspx
原创地址:http://wutheringsea.iteye.com/blog/647924

分享到:
评论

相关推荐

    springboot 基础简易实例, maven项目

    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt; &lt;version&gt;2.1.4.RELEASE&lt;/version&gt; &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt; &lt;/parent&gt; &lt;groupId&gt;com.example&lt;/groupId&gt; &lt;artifactId&gt;...

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

    &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;!-- 字符集 过滤器 --&gt; &lt;filter&gt; &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt; &lt;filter-class...

    DWR中文文档.pdf

    util.js 功能 54&lt;br/&gt;7.1 $() 54&lt;br/&gt;7.2 addOptions and removeAllOptions 54&lt;br/&gt;7.3 addRows and removeAllRows 55&lt;br/&gt;7.4 getText 56&lt;br/&gt;7.5 getValue 56&lt;br/&gt;7.6 getValues 57&lt;br/&gt;7.7 onReturn 57&lt;br/&gt;7.8 ...

    cas 配置client 1.0 &2.0 及proxy DEMO 说明

    &lt;filter-name&gt;CAS Filter&lt;/filter-name&gt; &lt;filter-class&gt; edu.yale.its.tp.cas.client.filter.CASFilter &lt;/filter-class&gt; &lt;!-- server login url --&gt; &lt;init-param&gt; &lt;param-name&gt; edu.yale.its.tp...

    SpringMVC-SSH全注解

    &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;property name="packagesToScan"&gt; ...

    CXF WebService整合Spring示例工程代码demo

    &lt;listener-class&gt;org.springframework.web.util.IntrospectorCleanupListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;servlet&gt; &lt;servlet-name&gt;CXFService&lt;/servlet-name&gt; &lt;servlet-class&gt;org.apache.cxf.transport...

    spring_MVC源码

    09. &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; 10. &lt;/listener&gt; 11. 12. &lt;servlet&gt; 13. &lt;servlet-name&gt;spring&lt;/servlet-name&gt; 14. &lt;servlet-class&gt;org.spring...

    spring-boot-集成mybatis带分页page

    &lt;groupId&gt;org.mybatis.spring.boot&lt;/groupId&gt; &lt;artifactId&gt;mybatis-spring-boot-starter&lt;/artifactId&gt; &lt;version&gt;1.1.1&lt;/version&gt; &lt;/dependency&gt; &lt;!--mapper--&gt; &lt;dependency&gt; &lt;groupId&gt;tk.mybatis&lt;/groupId...

    maven的优缺点 项目

    -- &lt;version&gt;3.6.1&lt;/version&gt; --&gt; &lt;configuration&gt; &lt;source&gt;1.7&lt;/source&gt; &lt;target&gt;1.7&lt;/target&gt; &lt;encoding&gt;UTF-8&lt;/encoding&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;!-- 将源码打包成JAR包 -...

    Spring实战之使用util:命名空间简化配置操作示例

    本文实例讲述了Spring使用util:命名空间简化配置操作。分享给大家供大家参考,具体如下: 一 配置 &lt;?xml version=1.0 encoding=GBK?&gt; &lt;!-- 指定Spring配置文件的根元素和Schema 导入p:命名空间和util:...

    freemarker总结

    其它的运行符可以作用于数字和日期,但不能作用于字符串,大部分的时候,使用gt等字母运算符代替&gt;会有更好的效果,因为 FreeMarker会把&gt;解释成FTL标签的结束字符,当然,也可以使用括号来避免这种情况,如:&lt;#if (x&gt;y)&gt; ...

    tomcat8 + nginx + memcached + cas 实现负载均衡的配置包

    1.2添加memcached如下依赖的jar包到tomcat8.x/lib/: asm-5.1.jar couchbase-client-1.4.12.jar javolution-5.5.1.jar kryo-4.0.0.jar kryo-serializers-0.38.jar memcached-session-manager-2.0.0.jar memcached...

    spring-boot-util:用于Spring启动项目的实用程序库

    &lt;artifactId&gt;spring-boot-util&lt;/artifactId&gt; &lt;&lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt; &lt;/dependency&gt; 2.配置IP安全性属性 启用IP安全性 ipSecurity.enabled=true 注意:默认为ipSecurity.enabled=false 。 通过CI

    ssh(structs,spring,hibernate)框架中的上传下载

     由于Spring通过代理Hibernate完成数据层的操作,所以原Hibernate的配置文件hibernate.cfg.xml的信息也转移到Spring的配置文件中:  代码 4 Spring中有关Hibernate的配置信息 1. <beans> 2. <!-- 数据源的配置 ...

    springmybatis

    查询出列表,也就是返回list, 在我们这个例子中也就是 List&lt;User&gt; , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...

    拦截器和控制器的区别

    &lt;interceptor name="myInterceptor" class="util.MyInterceptor"&gt;&lt;/interceptor&gt; 定义拦截器的值栈空间 &lt;interceptor-stack name="myStack"&gt; &lt;interceptor-ref name="defaultStack"&gt;&lt;/interceptor-ref&gt; ...

    DWR开发包常规客户端服务器端面向对象编程

    &lt;script type='text/javascript' src='&lt;%=request.getContextPath()%&gt;/dwr/util.js'&gt;&lt;/script&gt; 客户端调用JAVA基本类: function getJavaDate(){ JDate.getYear(load); } function load(data){ alert(data+...

    24-springboot 测试.rar

    微服务架构集大成者—Spring Cloud https://www.jianshu.com/p/3899d7f47303 api首页路径:http://192.168.0.107:9090/swagger-ui.html //端口可以在配置文件修改, F:\test\java\java_learn\idea_work\...

    Spring-Reference_zh_CN(Spring中文参考手册)

    9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.6.1. @Transactional 有关的设置 9.5.7. 插入事务操作 9.5.8. 结合AspectJ使用 @Transactional 9.6. 编程式事务管理 9.6.1. 使用 ...

    SpringShiro分布式缓存版

    &lt;bean id="redisSessionDAO" class="hxk.util.redis.RedisSessionDAO"&gt; &lt;property name="redisManager" ref="redisManager"/&gt; &lt;property name="timeToLiveSeconds" value="180"/&gt; &lt;/bean&gt; &lt;!-- 配置shiro提供...

Global site tag (gtag.js) - Google Analytics