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

yipeiwu_com7年前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程序设计有所帮助。

相关文章

python3 http提交json参数并获取返回值的方法

如下所示: import json import http.client connection = http.client.HTTPSConnection('spd.aiopos...

Python操作json数据的一个简单例子

更多的信息,可以参考python内部的json文档: python>>> help(json) 或者官方文档: http://docs.python.org/lib...

MAC中PyCharm设置python3解释器

MAC中PyCharm设置python3解释器

MAC上的PyCharm中默认的python解释器是python2的,windows下的没用过不是很清楚,所以特来记录下设置python3解释器的过程。 python3的查找与安装 如果...

Python中函数参数设置及使用的学习笔记

Python中函数参数设置及使用的学习笔记

一、参数和共享引用: In [56]: def changer(a,b): ....: a=2 ....: b[0]='spam' ....: In [57...

python实现学生管理系统

python实现学生管理系统

python写的简单的学生管理系统,练习python语法。 可以运行在windows和linux下,python 2.7。 #!/usr/local/bin/python #...