Spring實(shí)戰(zhàn)之使用util:命名空間簡化配置操作示例
本文實(shí)例講述了Spring使用util:命名空間簡化配置操作。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?> <!-- 指定Spring配置文件的根元素和Schema 導(dǎo)入p:命名空間和util:命名空間的元素 --> <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-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置chinese實(shí)例,其實(shí)現(xiàn)類是Chinese --> <bean id="chinese" class="org.crazyit.app.service.impl.Chinese" p:age-ref="chin.age" p:axe-ref="stoneAxe" p:schools-ref="chin.schools" p:axes-ref="chin.axes" p:scores-ref="chin.scores"/> <!-- 使用util:constant將指定類的靜態(tài)Field定義成容器中的Bean --> <util:constant id="chin.age" static-field= "java.sql.Connection.TRANSACTION_SERIALIZABLE"/> <!-- 使用util.properties加載指定資源文件 --> <util:properties id="confTest" location="classpath:test_zh_CN.properties"/> <!-- 使用util:list定義一個List集合,指定使用LinkedList作為實(shí)現(xiàn)類, 如果不指定默認(rèn)使用ArrayList作為實(shí)現(xiàn)類 --> <util:list id="chin.schools" list-class="java.util.LinkedList"> <!-- 每個value、ref、bean...配置一個List元素 --> <value>小學(xué)</value> <value>中學(xué)</value> <value>大學(xué)</value> </util:list> <!-- 使用util:set定義一個Set集合,指定使用HashSet作為實(shí)現(xiàn)類, 如果不指定默認(rèn)使用HashSet作為實(shí)現(xiàn)類--> <util:set id="chin.axes" set-class="java.util.HashSet"> <!-- 每個value、ref、bean...配置一個Set元素 --> <value>字符串</value> <bean class="org.crazyit.app.service.impl.SteelAxe"/> <ref bean="stoneAxe"/> </util:set> <!-- 使用util:map定義一個Map集合,指定使用TreeMap作為實(shí)現(xiàn)類, 如果不指定默認(rèn)使用HashMap作為實(shí)現(xiàn)類 --> <util:map id="chin.scores" map-class="java.util.TreeMap"> <entry key="數(shù)學(xué)" value="87"/> <entry key="英語" value="89"/> <entry key="語文" value="82"/> </util:map> <!-- 配置steelAxe實(shí)例,其實(shí)現(xiàn)類是SteelAxe --> <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/> <!-- 配置stoneAxe實(shí)例,其實(shí)現(xiàn)類是StoneAxe --> <bean id="stoneAxe" class="org.crazyit.app.service.impl.StoneAxe"/> </beans>
二 接口
Axe
package org.crazyit.app.service; public interface Axe { // Axe接口里有個砍的方法 public String chop(); }
Person
package org.crazyit.app.service; public interface Person { // 定義一個使用斧子的方法 public void useAxe(); }
三 實(shí)現(xiàn)
Chinese
package org.crazyit.app.service.impl; import java.util.*; import org.crazyit.app.service.*; public class Chinese implements Person { private Axe axe; private int age; private List schools; private Map scores; private Set axes; // axe的setter方法 public void setAxe(Axe axe) { this.axe = axe; } // age的setter方法 public void setAge(int age) { this.age = age; } // schools的setter方法 public void setSchools(List schools) { this.schools = schools; } // scores的setter方法 public void setScores(Map scores) { this.scores = scores; } // axes的setter方法 public void setAxes(Set axes) { this.axes = axes; } // 實(shí)現(xiàn)Person接口的useAxe()方法 public void useAxe() { System.out.println(axe.chop()); System.out.println("age屬性值:" + age); System.out.println(schools); System.out.println(scores); System.out.println(axes); } }
StoneAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class StoneAxe implements Axe { public String chop() { return "石斧砍柴好慢"; } }
SteelAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class SteelAxe implements Axe { public String chop() { return "鋼斧砍柴真快"; } }
四 測試類
package lee; import org.springframework.context.*; import org.springframework.context.support.*; import org.crazyit.app.service.*; public class BeanTest { public static void main(String[] args) { // 創(chuàng)建Spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 獲取chinese實(shí)例 Person p = ctx.getBean("chinese" , Person.class); // 調(diào)用useAxe()方法 p.useAxe(); System.out.println(ctx.getBean("confTest")); } }
五 資源文件
a=\u8f7b\u91cf\u7ea7Java EE\u4f01\u4e1a\u5e94\u7528\u5b9e\u6218 b=\u75af\u72c2Java\u8bb2\u4e49
六 運(yùn)行
石斧砍柴好慢
age屬性值:8
[小學(xué), 中學(xué), 大學(xué)]
{數(shù)學(xué)=87, 英語=89, 語文=82}
[字符串, org.crazyit.app.service.impl.SteelAxe@eec5a4a, org.crazyit.app.service.impl.StoneAxe@2b2948e2]
{b=瘋狂Java講義, a=輕量級Java EE企業(yè)應(yīng)用實(shí)戰(zhàn)}
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
把MySQL表結(jié)構(gòu)映射為Python中的對象的教程
這篇文章主要介紹了簡單地把MySQL表結(jié)構(gòu)映射為Python中的對象的方法,用到了Python中的SQLAlchemy庫,需要的朋友可以參考下2015-04-04python 環(huán)境安裝及編輯器配置方法小結(jié)
這篇文章主要介紹了python 環(huán)境安裝及編輯器配置方法小結(jié)的相關(guān)資料,需要的朋友可以參考下2021-06-06