Spring實(shí)戰(zhàn)之使用util:命名空間簡(jiǎn)化配置操作示例
本文實(shí)例講述了Spring使用util:命名空間簡(jiǎn)化配置操作。分享給大家供大家參考,具體如下:
一 配置
<?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定義一個(gè)List集合,指定使用LinkedList作為實(shí)現(xiàn)類, 如果不指定默認(rèn)使用ArrayList作為實(shí)現(xiàn)類 --> <util:list id="chin.schools" list-class="java.util.LinkedList"> <!-- 每個(gè)value、ref、bean...配置一個(gè)List元素 --> <value>小學(xué)</value> <value>中學(xué)</value> <value>大學(xué)</value> </util:list> <!-- 使用util:set定義一個(gè)Set集合,指定使用HashSet作為實(shí)現(xiàn)類, 如果不指定默認(rèn)使用HashSet作為實(shí)現(xiàn)類--> <util:set id="chin.axes" set-class="java.util.HashSet"> <!-- 每個(gè)value、ref、bean...配置一個(gè)Set元素 --> <value>字符串</value> <bean class="org.crazyit.app.service.impl.SteelAxe"/> <ref bean="stoneAxe"/> </util:set> <!-- 使用util:map定義一個(gè)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="英語(yǔ)" value="89"/> <entry key="語(yǔ)文" 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接口里有個(gè)砍的方法 public String chop(); }
Person
package org.crazyit.app.service; public interface Person { // 定義一個(gè)使用斧子的方法 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 "鋼斧砍柴真快"; } }
四 測(cè)試類
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, 英語(yǔ)=89, 語(yǔ)文=82}
[字符串, org.crazyit.app.service.impl.SteelAxe@eec5a4a, org.crazyit.app.service.impl.StoneAxe@2b2948e2]
{b=瘋狂Java講義, a=輕量級(jí)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緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
pandas與pyspark計(jì)算效率對(duì)比分析
這篇文章主要介紹了pandas與pyspark計(jì)算效率對(duì)比,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-063分鐘學(xué)會(huì)一個(gè)Python小技巧
Python時(shí)間日期轉(zhuǎn)換在開(kāi)發(fā)中是非常高頻的一個(gè)操作,你經(jīng)常會(huì)遇到需要將字符串轉(zhuǎn)換成 datetime 或者是反過(guò)來(lái)將 datetime 轉(zhuǎn)換成字符串,今天小編給大家?guī)?lái)了一個(gè)Python小技巧,感興趣的朋友一起看看吧2018-11-11如何基于python測(cè)量代碼運(yùn)行時(shí)間
這篇文章主要介紹了如何基于python測(cè)量代碼運(yùn)行時(shí)間,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Python psutil模塊簡(jiǎn)單使用實(shí)例
這篇文章主要介紹了Python psutil模塊簡(jiǎn)單使用實(shí)例,本文直接給出使用腳本,實(shí)現(xiàn)查看cpu的信息、查看內(nèi)存信息、查看系統(tǒng)啟動(dòng)時(shí)間、查看網(wǎng)卡信息等,需要的朋友可以參考下2015-04-04詳解Python中pandas的安裝操作說(shuō)明(傻瓜版)
這篇文章主要介紹了Python中pandas的安裝操作說(shuō)明,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04把MySQL表結(jié)構(gòu)映射為Python中的對(duì)象的教程
這篇文章主要介紹了簡(jiǎn)單地把MySQL表結(jié)構(gòu)映射為Python中的對(duì)象的方法,用到了Python中的SQLAlchemy庫(kù),需要的朋友可以參考下2015-04-04給Python中的MySQLdb模塊添加超時(shí)功能的教程
這篇文章主要介紹了給Python中的MySQLdb模塊添加超時(shí)功能的教程,timeout功能在服務(wù)器的運(yùn)維當(dāng)中非常有用,需要的朋友可以參考下2015-05-05python 環(huán)境安裝及編輯器配置方法小結(jié)
這篇文章主要介紹了python 環(huán)境安裝及編輯器配置方法小結(jié)的相關(guān)資料,需要的朋友可以參考下2021-06-06