7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式
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... 基于XML Schema的简化配置方式的更多相关文章
- spring-第十篇之XML Schema的简化配置,p、c、util命名空间
1.p:命名空间 引入命名空间:xmlns:p="http://www.springframework.org/schema/p" 配置举例: <?xml version=& ...
- 7 -- Spring的基本用法 -- 11...
7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...
- Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...
- Spring Aop(七)——基于XML配置的Spring Aop
转发:https://www.iteye.com/blog/elim-2396043 7 基于XML配置的Spring AOP 基于XML配置的Spring AOP需要引入AOP配置的Schema,然 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation
AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...
- 使用Spring框架入门一:基于XML配置的IOC/DI的使用
一.Spring框架 1.方法一:逐项导入基础依赖包: spring-core.spring-beans.spring-context.spring-expression 2.方法二:最简洁的导入,直 ...
- 基于xml文件的格式配置Spring的AOP
用例子直接说明: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...
- 解释基于XML Schema方式的切面实现?
在这种情况下,切面由常规类以及基于XML的配置实现.
- 解释基于 XML Schema 方式的切面实现?
在这种情况下,切面由常规类以及基于 XML 的配置实现.
随机推荐
- PCL点云特征描述与提取(3)
快速点特征直方图(FPFH)描述子 已知点云P中有n个点,那么它的点特征直方图(PFH)的理论计算复杂度是,其中k是点云P中每个点p计算特征向量时考虑的邻域数量.对于实时应用或接近实时应用中,密集点云 ...
- Homebrew替换源
https://www.zhihu.com/question/31360766/answer/74155248 以及 https://www.zhihu.com/question/31360766
- SAP 以工序为基准进行发料 机加工行业 Goods Issue to Routing
SAP 以工序为基准进行发料 这个流程是在业务有关需求,业务需要按照工序发料,一个工单有多个工序,而料是要发到每个工序上,而且没到工序之间在物理上是有距离的,所以仓管员在打印配发单之后希望了解到哪 ...
- vector 用法小例子
1. vector<int> vec; vec.push_back(1); 2. vector<int> vec(10); vec[0] = 1;
- zXing使用注意事项-转
zXing使用注意事项 ------------------ zxing和zbar的比较: ZXing项目的示例程序对于摄像头的控制写的非常全面,ZBar的没有ZBar基于C语言编写,解码效率高于 ...
- 「下载神器」aria2 懒人安装教程 [Windows]
是一款开源.轻量级的多协议命令行下载工具,支持 HTTP/HTTPS.FTP.SFTP.BitTorrent 和 Metalink 协议,拥有众多第三方支持插件,被誉为「下一代下载工具」和「下载神器」 ...
- 启动其他APK的Activity方法 (转至http://www.cnblogs.com/lijunamneg/archive/2013/02/26/2934060.html)
有两个app,分别叫做App1和App2.App1包含两个Activity,分别叫做App1_A和App1_B.其中App1_A是入口Activity.也就是App1_A设置intent-filter ...
- C# HttpClient请求
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System ...
- A:LinkedList实现了List接口; B: AbstractSet实现了Set接口; C: HashSet继承自AbstractSet基类; D: WeakMap继承自 AbstractMap
List,Set,Map在java.util包下都是接口 List有两个实现类:ArrayList和LinkedListSet有两个实现类:HashSet和LinkedHashSetAbstractS ...
- 8.4 Java 命名规范
Class: 所有单词的首字母大写, TestJava Method: 第一个单词的首字母小写,之后每个单词的首字母大写, getAddressForMe() Property: 第一个单词的首字母小 ...