溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring實戰之使用util:命名空間簡化配置操作示例

發布時間:2020-09-26 04:01:23 來源:腳本之家 閱讀:216 作者:cakincqm 欄目:開發技術

本文實例講述了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 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將指定類的靜態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作為實現類,
   如果不指定默認使用ArrayList作為實現類 -->
   <util:list id="chin.schools" list-class="java.util.LinkedList">
      <!-- 每個value、ref、bean...配置一個List元素 -->
      <value>小學</value>
      <value>中學</value>
      <value>大學</value>
   </util:list>
   <!-- 使用util:set定義一個Set集合,指定使用HashSet作為實現類,
   如果不指定默認使用HashSet作為實現類-->
   <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作為實現類,
   如果不指定默認使用HashMap作為實現類 -->
   <util:map id="chin.scores" map-class="java.util.TreeMap">
      <entry key="數學" value="87"/>
      <entry key="英語" value="89"/>
      <entry key="語文" value="82"/>
   </util:map>
   <!-- 配置steelAxe實例,其實現類是SteelAxe -->
   <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
   <!-- 配置stoneAxe實例,其實現類是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();
}

三 實現

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;
  }
  // 實現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)
  {
    // 創建Spring容器
    ApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    // 獲取chinese實例
    Person p = ctx.getBean("chinese" , Person.class);
    // 調用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

六 運行

石斧砍柴好慢
age屬性值:8
[小學, 中學, 大學]
{數學=87, 英語=89, 語文=82}
[字符串, org.crazyit.app.service.impl.SteelAxe@eec5a4a,  org.crazyit.app.service.impl.StoneAxe@2b2948e2]
{b=瘋狂Java講義, a=輕量級Java EE企業應用實戰}

更多關于java相關內容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數據結構與算法教程》、《Java操作DOM節點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》

希望本文所述對大家java程序設計有所幫助。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女