关于spring中util:/的配置
解決redis設置緩存時間找到的帖子,我這個初學者需要學習的還是很多的。
原文地址:http://www.doc100.net/bugs/t/216322/index.html
探索<util/>命名空間 ???? 事情的發展總是一段曲折前進的過程。當Spring剛出現時,開發者可以使用<list/>、<map/>、<set/>等元素定義集合,然而這些集合不能夠在不同的受管Bean間進行復用。盡管開發者可以采用抽象Bean機制實現復用,但實在不怎么優雅。與此同時,開發者借助ListFactoryBean、MapFactoryBean和SetFactoryBean等對象能夠定義出可供復用的集合。然而,這也不是很友好的做法。再后來,<util/>命名空間被Spring 2.x引入,這才使得集合的定義變得簡單。
首先在spring的配置文件中添加
?
1. <util: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屬性文件.??
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/>元素
? ?? 繼承了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
轉載于:https://www.cnblogs.com/zypu/p/6593827.html
總結
以上是生活随笔為你收集整理的关于spring中util:/的配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dropMenu----简单的下拉菜单生
- 下一篇: 孕晚期梦到老鼠是什么意思