本文實例講述了Spring使用util:命名空間簡化配置操作。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?><!-- 指定Spring配置文件的根元素和Schema 導入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實例,其實現類是Chinese --> <bean 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將指定類的靜態Field定義成容器中的Bean --> <util:constant static-field= "java.sql.Connection.TRANSACTION_SERIALIZABLE"/> <!-- 使用util.properties加載指定資源文件 --> <util:properties location="classpath:test_zh_CN.properties"/> <!-- 使用util:list定義一個List集合,指定使用LinkedList作為實現類, 如果不指定默認使用ArrayList作為實現類 --> <util:list list-class="java.util.LinkedList"> <!-- 每個value、ref、bean...配置一個List元素 --> <value>小學</value> <value>中學</value> <value>大學</value> </util:list> <!-- 使用util:set定義一個Set集合,指定使用HashSet作為實現類, 如果不指定默認使用HashSet作為實現類--> <util:set 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作為實現類, 如果不指定默認使用HashMap作為實現類 --> <util:map map-class="java.util.TreeMap"> <entry key="數學" value="87"/> <entry key="英語" value="89"/> <entry key="語文" value="82"/> </util:map> <!-- 配置steelAxe實例,其實現類是SteelAxe --> <bean class="org.crazyit.app.service.impl.SteelAxe"/> <!-- 配置stoneAxe實例,其實現類是StoneAxe --> <bean 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();}
三 實現
Chinese
新聞熱點
疑難解答