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

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

(0)

相关推荐

  • Spring MVC 框架搭建配置方法及详解

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过要想灵活运用Spring MVC来应对大多数的Web开发,就必须要掌握它的配置及原理. 一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.jar.comm

  • SpringBoot + Spring Security 基本使用及个性化登录配置详解

    Spring Security 基本介绍 这里就不对Spring Security进行过多的介绍了,具体的可以参考官方文档 我就只说下SpringSecurity核心功能: 认证(你是谁) 授权(你能干什么) 攻击防护(防止伪造身份) 基本环境搭建 这里我们以SpringBoot作为项目的基本框架,我这里使用的是maven的方式来进行的包管理,所以这里先给出集成Spring Security的方式 <dependencies> ... <dependency> <groupI

  • 在SpringBoot下读取自定义properties配置文件的方法

    SpringBoot工程默认读取application.properties配置文件.如果需要自定义properties文件,如何读取呢? 一.在resource中新建.properties文件 在resource目录下新建一个config文件夹,然后新建一个.properties文件放在该文件夹下.如图remote.properties所示 二.编写配置文件 remote.uploadFilesUrl=/resource/files/ remote.uploadPicUrl=/resource

  • spring boot Logging的配置以及使用详解

    前言:该篇文章基本上是翻译的官方文档! spring boot使用Commons Logging作为内部的日志系统,并且给Java Util Logging,Log4J2以及Logback都提供了默认的配置.如果使用了spring boot的Starters,那么默认会使用Logback用于记录日志. 一.Log format spring boot中默认的日志输出格式如下: 2014-03-05 10:57:51.112 INFO 45469 --- [ main] org.apache.ca

  • Spring boot 默认静态资源路径与手动配置访问路径的方法

    在application.propertis中配置 ##端口号 server.port=8081 ##默认前缀 spring.mvc.view.prefix=/ ## 响应页面默认后缀 spring.mvc.view.suffix=.html # 默认值为 /** spring.mvc.static-path-pattern=/** # 这里设置要指向的路径,多个使用英文逗号隔开,默认值为 classpath:/META-INF/resources/,classpath:/resources/,

  • spring boot加载资源路径配置和classpath问题解决

    1.spring boot默认加载文件的路径: /META-INF/resources/ /resources/ /static/ /public/ 我们也可以从spring boot源码也可以看到: private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classp

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

    本文实例讲述了Spring实战之使用p:命名空间简化配置操作.分享给大家供大家参考,具体如下: 一 配置 <?xml version="1.0" encoding="GBK"?> <!-- 指定Spring配置文件的根元素和Schema 并导入p:命名空间的元素 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="

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

    本文实例讲述了Spring使用c命名空间简化配置操作.分享给大家供大家参考,具体如下: 一 配置 <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:c

  • 详解SpringBoot配置连接池

    内置的连接池 目前spring Boot中默认支持的连接池有dbcp,dbcp2, tomcat, hikari三种连接池. 数据库连接可以使用DataSource池进行自动配置. 由于Tomcat数据源连接池的性能和并发,在tomcat可用时,我们总是优先使用它. 如果HikariCP可用,我们将使用它. 如果Commons DBCP可用,我们将使用它,但在生产环境不推荐使用它. 最后,如果Commons DBCP2可用,我们将使用它. 以上的几种连接池,可以通过在配置application文

  • 读取spring配置文件的方法(spring读取资源文件)

    1.spring配置文件 复制代码 代码如下: <bean id="configproperties"          class="org.springframework.beans.factory.config.PropertiesFactoryBean">          <property name="location" value="classpath:jdbc.properties"/>

  • SpringBoot入坑笔记之spring-boot-starter-web 配置文件的使用

    经过上一篇的介绍,相信小伙伴们已经按奈不住内心对springboot的向往,本篇我将继续向小伙伴介绍springboot配置文件的配置,已经全局配置参数如何使用,好了下面开始我们今天的内容介绍. 我们知道Spring Boot支持容器的自动配置,默认是Tomcat,当然我们也是可以进行修改的: 1.首先我们排除spring-boot-starter-web依赖中的Tomcat:在pom文件中排除tomcat的starter <dependency> <groupId>org.spr

随机推荐