Spring实战之使用util:命名空间简化配置操作示例

yipeiwu_com5年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

python利用datetime模块计算时间差

今天写了点东西,要计算时间差,我记得去年写过,于是今天再次mark一下,以免自己忘记 In [27]: from datetime import datetime In [28]:...

PYTHON正则表达式 re模块使用说明

首先,运行 Python 解释器,导入 re 模块并编译一个 RE: #!python Python 2.2.2 (#1, Feb 10 2003, 12:57:01) >>...

python获取程序执行文件路径的方法(推荐)

python获取程序执行文件路径的方法(推荐)

1.获取当前执行主脚本方法:sys.argv[0]和_ file _ (1)sys.argv 一个传给Python脚本的指令参数列表。sys.argv[0]是脚本的名字。一般得到的是...

全面理解Python中self的用法

刚开始学习Python的类写法的时候觉得很是麻烦,为什么定义时需要而调用时又不需要,为什么不能内部简化从而减少我们敲击键盘的次数?你看完这篇文章后就会明白所有的疑问。 self代表类的实...

PyQt5打开文件对话框QFileDialog实例代码

本文研究的主要是PyQt5打开文件对话框QFileDialog的代码示例,具体如下。 单个文件打开 QFileDialog.getOpenFileName() 多个文件打开 QFile...