日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

关于spring中util:/的配置

發(fā)布時(shí)間:2023/11/29 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于spring中util:/的配置 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

解決redis設(shè)置緩存時(shí)間找到的帖子,我這個(gè)初學(xué)者需要學(xué)習(xí)的還是很多的。

原文地址:http://www.doc100.net/bugs/t/216322/index.html

探索<util/>命名空間 ???? 事情的發(fā)展總是一段曲折前進(jìn)的過程。當(dāng)Spring剛出現(xiàn)時(shí),開發(fā)者可以使用<list/>、<map/>、<set/>等元素定義集合,然而這些集合不能夠在不同的受管Bean間進(jìn)行復(fù)用。盡管開發(fā)者可以采用抽象Bean機(jī)制實(shí)現(xiàn)復(fù)用,但實(shí)在不怎么優(yōu)雅。與此同時(shí),開發(fā)者借助ListFactoryBean、MapFactoryBean和SetFactoryBean等對(duì)象能夠定義出可供復(fù)用的集合。然而,這也不是很友好的做法。再后來,<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 <a href="http://www.springframework.org/schema/util/spring-util-2.0.xsd">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進(jìn)行管理,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啟動(dòng)初始化bean時(shí),給listUtil這個(gè)list賦值為這四個(gè)值。 下面的同理
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對(duì)象的使用,可使用如下代碼 :

<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對(duì)象,可使用如下代碼 :

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

??? 在導(dǎo)入了</p>命名空間后,等價(jià)于??

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

???? ?????? 實(shí)例:http://blog.csdn.net/daryl715/archive/2007/09/26/1802292.aspx

轉(zhuǎn)載于:https://www.cnblogs.com/zypu/p/6593827.html

總結(jié)

以上是生活随笔為你收集整理的关于spring中util:/的配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。