7.11 基于XML Schema的简化配置方式

      Spring允许使用基于XML Schema的配置方式来简化Spring配置文件。

      7.11.1 使用p:命名空间简化配置

        p:命名空间不需要特定的Schema定义,它直接存在于Spring内核中。

        当导入p:命名空间之后,就可直接在<bean.../>元素中使用属性来驱动执行setter方法。即用于简化设置注入

        Class : SteelAxe

package edu.pri.lime._7_11_1.bean;

public class SteelAxe implements Axe {

    public String chop() {
return "用钢斧砍材真快";
} }

        Class : Chinese

package edu.pri.lime._7_11_1.bean.impl;

import edu.pri.lime._7_11_1.bean.Axe;
import edu.pri.lime._7_11_1.bean.Person; public class Chinese implements Person{ private Axe axe;
private int age;
public Chinese() {
super();
// TODO Auto-generated constructor stub
}
public Axe getAxe() {
return axe;
}
public void setAxe(Axe axe) {
this.axe = axe;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void useAxe(){
System.out.println(axe.chop());
System.out.println("age成员变量的值:" + age);
} }

        导入XML Schema里的p:命名空间

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
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">

        XML :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
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"> <!-- 配置steelAxe实例,其实现类是SteelAxe -->
<bean id="steelAxe" class="edu.pri.lime._7_11_1.bean.SteelAxe"/>
<!-- 配置chinese实例,其实现类是Chinese -->
<!-- 使用属性配置对age、axe执行设置注入 -->
<!-- 使用p:命名空间时,Bean的属性名不能以-ref结尾 -->
<bean id="chinese" class="edu.pri.lime._7_11_1.bean.impl.Chinese" p:age="30" p:axe-ref="steelAxe" />
</beans>

        Class :SpringTest

package edu.pri.lime._7_11_1.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import edu.pri.lime._7_11_1.bean.Person;
import edu.pri.lime._7_11_1.bean.impl.Chinese; public class SpringTest { public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("app_7_11_1.xml");
Person chinese = ctx.getBean("chinese",Chinese.class);
chinese.useAxe();
}
}

        Console :

用钢斧砍材真快
age成员变量的值:30

      7.11.2 使用c:命名空间简化配置

        c:命名空间用于简化构造注入

        Class : Chinese

package edu.pri.lime._7_11_2.bean.impl;

import edu.pri.lime._7_11_2.bean.Axe;
import edu.pri.lime._7_11_2.bean.Person; public class Chinese implements Person{ private Axe axe;
private int age; public Chinese() {
super();
}
// 构造注入所需的带参数的构造器
public Chinese(Axe axe, int age) {
super();
this.axe = axe;
this.age = age;
} public void useAxe() {
System.out.println(axe.chop());
System.out.println("age成员变量的值:" + age); }
public Axe getAxe() {
return axe;
}
public void setAxe(Axe axe) {
this.axe = axe;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }

        Class : SteelAxe

package edu.pri.lime._7_11_2.bean.impl;

import edu.pri.lime._7_11_2.bean.Axe;

public class SteelAxe implements Axe{

    public String chop() {
return "用钢斧砍材真快";
} }

        导入c:命名空间

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
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">

        XML :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
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"> <!-- 配置steelAxe实例,其实现类是SteelAxe -->
<bean id="steelAxe" class="edu.pri.lime._7_11_2.bean.impl.SteelAxe"/>
<!-- 配置chinese实例,其实现类是Chinese -->
<!-- 使用属性配置对age、axe执行设置注入 -->
<!-- 使用p:命名空间时,Bean的属性名不能以-ref结尾 -->
<bean id="chinese" class="edu.pri.lime._7_11_2.bean.impl.Chinese" c:age="30" c:axe-ref="steelAxe" />
</beans>

        Class : SpringTest

package edu.pri.lime._7_11_2.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import edu.pri.lime._7_11_2.bean.Person;
import edu.pri.lime._7_11_2.bean.impl.Chinese; public class SpringTest { public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("app_7_11_2.xml");
Person chinese = ctx.getBean("chinese",Chinese.class);
chinese.useAxe();
}
}

        使用c:指定构造器参数的格式为:c:构造器参数名=“值” 或 c:构造器参数名-ref=“其他Bean的id”。

        通过索引来配置构造器参数的方式:

<bean id="chinese" class="edu.pri.lime._7_11_2.bean.impl.Chinese" c:_0-ref = "steelAxe" c:_1="21" />

        通过索引来配置构造器参数的格式:c:_N=“值” 或 c:_N-ref=“其他Bean的id” 其中N代表第N个构造器参数。

      7.11.3 使用util:命名空间简化配置

        在Spring框架解压缩包的schema\util\路径下包含有util:命名空间的XML Schema文件,为了使用util:命令空间的元素,必须先在Spring配置文件中导入最新的spring-util-4.0.xsd,也就是需要在Spring配置文件中增加如下配置:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
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">

        在util Schema下提供了如下几个元素:

          ⊙ constant :该元素用于获取指定类的静态Field的值。它是FieldRetrievingFactoryBean的简化配置。

          ⊙ property-path : 该元素用于获取指定对象的getter方法的返回值。它是PropertyPathFactoryBean的简化配置。

          ⊙ list : 该元素用于定义一个List Bean,支持使用<value.../>、<ref.../>、<bean.../>等子元素来定义List集合元素。

            使用该标签支持如下三个属性:

            ◎ id : 该属性指定定义一个名为id 的List Bean实例。

            ◎ list-class : 该属性指定Spring使用哪个List实现类来创建Bean实例。默认使用ArrayList作为实现类。

            ◎ scope : 指定该List Bean实例的作用域。

          ⊙ set : 该元素用于定义一个Set Bean,支持使用<value.../>、<ref.../>、<bean.../>等子元素来定义Set元素集合。

            ◎ id : 该属性指定定义一个名为id的Set Bean实例。

            ◎ set-class : 该属性指定Spring 使用哪个Set实现类来创建Bean实例。默认使用HashSet作为实现类。

            ◎ scope : 指定该Set Bean实例的作用域。

          ⊙ map : 该元素用于指定一个Map Bean,支持使用<entry.../>来定义Map的key-value对。

            使用该标签支持如下三个属性:

            ◎ id 该属性指定定义一个名为id的Map Bean实例。

            ◎ map-class : 该属性指定Spring使用哪个Map实现类来创建Bean实例。默认使用HashMap作为实现类。

            ◎ scope : 指定该Map Bean 实例的作用域。

          ⊙ properties : 该元素用于加载一份资源文件,并根据加载的资源文件创建一个Properties Ben 实例。

            使用该标签可指定如下几个属性:

            ◎ id : 该属性指定定义一个名为id 的Properties Bean实例。

            ◎ location : 指定资源文件的位置。

            ◎ scope : 指定该Properties Bean实例的作用域。

          Class : Chinese

package edu.pri.lime._7_11_3.bean.impl;

import java.util.List;
import java.util.Map;
import java.util.Set; import edu.pri.lime._7_11_3.bean.Axe;
import edu.pri.lime._7_11_3.bean.Person; public class Chinese implements Person{ private Axe axe;
private int age;
private List schools;
private Map scores;
private Set axes; public Chinese() {
super();
}
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);
}
public Axe getAxe() {
return axe;
}
public void setAxe(Axe axe) {
this.axe = axe;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List getSchools() {
return schools;
}
public void setSchools(List schools) {
this.schools = schools;
}
public Map getScores() {
return scores;
}
public void setScores(Map scores) {
this.scores = scores;
}
public Set getAxes() {
return axes;
}
public void setAxes(Set axes) {
this.axes = axes;
} }

        导入util:命名空间Schema

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
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">

        XML :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
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"> <bean id="steelAxe" class="edu.pri.lime._7_11_3.bean.impl.SteelAxe"/>
<util:list id="chin.schools" list-class="java.util.LinkedList">
<value>小学</value>
<value>中学</value>
<value>大学</value>
</util:list>
<util:map id="chin.scores" map-class="java.util.HashMap">
<entry key="数学" value="100"/>
<entry key="语文" value="100"/>
<entry key="英语" value="100"/>
</util:map>
<util:set id="chin.axes" set-class="java.util.HashSet">
<value>字符串</value>
<bean class="edu.pri.lime._7_11_3.bean.impl.SteelAxe"/>
<ref bean="chin.schools"/>
</util:set>
<bean id="chinese" class="edu.pri.lime._7_11_3.bean.impl.Chinese" p:axe-ref="steelAxe"
      p:age="20" p:schools-ref="chin.schools" p:scores-ref="chin.scores" p:axes-ref="chin.axes"/>
</beans>

    Class : SpringTest

package edu.pri.lime._7_11_3.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import edu.pri.lime._7_11_3.bean.Person;
import edu.pri.lime._7_11_3.bean.impl.Chinese; public class SpringTest { public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("app_7_11_3.xml");
Person chinese = ctx.getBean("chinese",Chinese.class);
chinese.useAxe();
}
}

        除此之外,关于Spring其他常用的简化Schema简要说明如下:

        ⊙ spring-aop-4.0.xsd :用于简化Spring AOP配置的Schema。

        ⊙ spring-jee-4.0.xsd : 用于简化Spring 的Java EE 配置的Schema。

        ⊙ spring-jms-4.0.xsd : 用于简化Spring关于JMS 配置的Schema。

        ⊙ spring-lang-4.0.xsd : 用于简化Spring 动态语言配置的Schema。

        ⊙ spring-tx-4.0.xsd : 用于简化Spring事务配置的Schema。

啦啦啦

7 -- Spring的基本用法 -- 11...的更多相关文章

  1. 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式

    7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...

  2. SpringMVC +mybatis+spring 结合easyui用法及常见问题总结

    SpringMVC +mybatis+spring 结合easyui用法及常见问题总结 1.FormatString的用法. 2.用postAjaxFillGrid实现dataGrid 把form表单 ...

  3. spring 第一篇(1-1):让java开发变得更简单(下)转

    spring 第一篇(1-1):让java开发变得更简单(下) 这个波主虽然只发了几篇,但是写的很好 上面一篇文章写的很好,其中提及到了Spring的jdbcTemplate,templet方式我之前 ...

  4. Spring MVC 学习笔记11 —— 后端返回json格式数据

    Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...

  5. Spring中@Async用法详解及简单实例

    Spring中@Async用法 引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类 ...

  6. Spring让人眼前一亮的11个小技巧

    前言 我们一说到spring,可能第一个想到的是 IOC(控制反转) 和 AOP(面向切面编程). 没错,它们是spring的基石,得益于它们的优秀设计,使得spring能够从众多优秀框架中脱颖而出. ...

  7. 7 -- Spring的基本用法 -- 4...

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  8. (转)Spring中@Async用法总结

     原文:http://blog.csdn.net/blueheart20/article/details/44648667 引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的: ...

  9. Spring中@Async用法总结

    引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3. ...

随机推荐

  1. 免费 WebOffice使用

    目前WebOffice使用比较多主要有两个公司的产品,分别是江西金格和北京点聚.但是点聚的是免费的,虽然有欠缺之处,但是经过个人修改还是比较好用的,关键一点是,它免费啊! 把一个最主要加载页面,如果读 ...

  2. PHP中json序列化后中文的编码显示问题

    在接口返回数据中,我们经常会返回json或者xml格式,php的json序列化函数json_encode非常好用,但是默认会把中文编码为ASCII码(注意,很多人认为这是乱码,其实不是),尤其在调试接 ...

  3. 最佳死链接href= "#" VS href= "javascript:void(0);"

    href= "#" 熟悉hash跳转的同学都知道,只是一个hash跳转,跳转到页面的顶部,可以发现地址栏多了一个#,点击返回会返回本页面. href= "javascri ...

  4. 格式化格林威治时间(Wed Aug 01 00:00:00 CST 2012)

    1.如果格林威治时间时间是date类型.(这种格式最简单)       SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd") ...

  5. Tree Restoring

    Tree Restoring Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement Aoki lo ...

  6. 查找mysql数据库中所有包含特定名字的字段所在的表

    整个数据库查找 placement 字段: select * from INFORMATION_SCHEMA.columns where COLUMN_NAME Like '%placement%'; ...

  7. php 批量导入数据的一种思维

    <?php $str="风湿免疫科 消化内科 内分泌科 神经内科 感染内科 心血管内科放疗中心";$arr=explode(' ',$str);$sql="&quo ...

  8. hdu 2544 最短路 (spfa)

    最短路 Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissio ...

  9. zencart url特殊字符处理

    1. 支持 在后台的seo url 将Outputw3c 改为false 2.删除特殊字符 这对于在少量的zen cart网站上处理少量的特殊字符可能还适用,实际上我们经常在导入产品数据时或者或少会带 ...

  10. 关于DOMContentLoaded

    也许还有朋友不太清楚DOMContentLoaded这个事件.简单的说,这个事件就是要在大多数情况下去替代window.onload事件,因为window.onload事件必须要等待页面所有元素的资源 ...