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基于PycURL自动处理cookie的方法

本文实例讲述了Python基于PycURL自动处理cookie的方法。分享给大家供大家参考。具体如下: import pycurl import StringIO url = "ht...

pytorch实现mnist数据集的图像可视化及保存

pytorch实现mnist数据集的图像可视化及保存

如何将pytorch中mnist数据集的图像可视化及保存 导出一些库 import torch import torchvision import torch.utils.data...

介绍Python中的__future__模块

Python的每个新版本都会增加一些新的功能,或者对原来的功能作一些改动。有些改动是不兼容旧版本的,也就是在当前版本运行正常的代码,到下一个版本运行就可能不正常了。 从Python 2....

Python中使用第三方库xlrd来写入Excel文件示例

继上一篇文章使用xlrd来读Excel之后,这一篇文章就来介绍下,如何来写Excel,写Excel我们需要使用第三方库xlwt,和xlrd一样,xlrd表示read xls,xlwt表示...

Python操作Redis之设置key的过期时间实例代码

Expire 命令用于设置 key 的过期时间。key 过期后将不再可用。 r.set('2', '4028b2883d3f5a8b013d57228d760a93') #成功就返回...