spring-beans-3.0.xsd是Spring的内核,其它的Schema大多都用于简化某些方面的配置
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<!-- xmlns:p是导入XML Schema里的p名字空间 -->
<!-- 指定spring配置文件的根元素和Schema 导入p空间和util命名空间的元素 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 配置chinese实例,其实现类是Chinese -->
<bean id="chinese" class="tju.chc.app.service.impl.Chinese"
p:axe-ref="stoneAxe" p:name="孙悟空"/>
<!-- 与上面等效<bean id="chinese" class="tju.chc.app.service.impl.Chinese">
<property name="axe" ref="stoneAxe"/>
<property name="name" value="孙悟空"/>
</bean>
-->
<!-- 配置stoneAxe实例,其实现类是StoneAxe -->
<bean id="stoneAxe" class="tju.chc.app.service.impl.StoneAxe"/>
<!-- 配置steelAxe实例,其实现类是SteelAxe -->
<bean id="steelAxe" class="tju.chc.app.service.impl.SteelAxe"/>
</beans>
在util Schema提供如下几个元素
1、constant:该标签用于将指定的静态Field暴露成一个Bean实例。使用该标签时可指定如下两个属性:
①id:该属性指定将静态Field定义成名为id的Bean实例
②static-field:该属性指定将哪个类,哪个静态Field暴露出来
2.property-path:该标签用于将指定Bean实例的指定属性暴露成一个Bean实例。使用该标签时可以指定如下两个属性:
①id:该属性指定将属性定义为名为id的Bean实例
②path:该属性指定将哪个Bean实例,哪个属性(支持复合属性)暴露出来
3、list:该标签用于定义一个List Bean,支持使用<value.../>,<ref.../><bean.../>等标签来定义List集合元素。使用该标签支持如下三个属性。
①id:该属性指定定义一个名为id的List Bean实例
②list-class:该实行指定Spring使用哪个List实例类来创建Bean实例
③scope:指定该List Bean实例的作用域
4.set:该标签用于定义一个Set Bean,支持<value.../>,<ref.../><bean.../>等标签来定义Set集合元素。使用该标签支持如下三个属性。
①id:该属性指定定义一个名为id的Set Bean实例
②set-class:该实行指定Spring使用哪个Set实例类来创建Bean实例
③scope:指定该Set Bean实例的作用域
5.map:该标签用于定义一个Map Bean,支持使用<entry.../>来定义Map的key-value对。使用该标签支持如下三个属性
①id:该属性指定定义一个名为id的Map Bean实例
②map-class:该实行指定Spring使用哪个map实例类来创建Bean实例
③scope:指定该map Bean实例的作用域
5.properties:该标签用于加载一份资源文件,并根据加载的资源文件创建一个Properties Bean实例。使用该标签以下属性:
①id:该属性指定定义一个名为id的Properties Bean实例
②location : 指定资文件的位置
③scope:指定该Properties Bean实例的作用域
例子如下
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<!-- xmlns:p是导入XML Schema里的p名字空间 -->
<!-- 指定spring配置文件的根元素和Schema 导入p空间和util命名空间的元素 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 配置american实例,其实现类是American -->
<bean id="american" class="tju.chc.app.service.impl.American"
p:axe-ref="stoneAxe" p:name="孙悟空" p:age-ref="ame.age" p:schools-ref="ame.schools"
p:axes-ref="ame.axes" p:scores-ref="ame.scores"/>
<!-- 使用util:constant将 指定类的静态Field暴露出来 -->
<util:constant id="ame.age" static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
<!-- 使用property-path 将指定Bean的指定属性暴露出来 -->
<util:property-path id="test" path="american.age"/>
<!-- 使用properties加载指定的资源 -->
<!-- <util:properties id="confTest" location="classpath:test_zh_CN.properties"/> -->
<!-- 使用util:list 定义一个List对象 -->
<util:list id="ame.schools" list-class="java.util.LinkedList">
<!-- 每个value ref bean都配置一个List元素 -->
<value>小学</value>
<value>中学</value>
<value>大学</value>
</util:list>
<!-- 使用util:set定义一个Set对象 -->
<util:set id="ame.axes" set-class="java.util.HashSet">
<!-- 每个value ref bean都配置一个List元素 -->
<bean class="tju.chc.app.service.impl.SteelAxe"/>
<ref local="stoneAxe"/>
</util:set>
<!-- 使用util:map定义一个Map对象 -->
<util:map id="ame.scores" map-class="java.util.TreeMap">
<entry key="数学" value="87"/>
<entry key="英语" value="87"/>
<entry key="语文" value="800"/>
</util:map>
<!-- 配置stoneAxe实例,其实现类是StoneAxe -->
<bean id="stoneAxe" class="tju.chc.app.service.impl.StoneAxe"/>
<!-- 配置steelAxe实例,其实现类是SteelAxe -->
<bean id="steelAxe" class="tju.chc.app.service.impl.SteelAxe"/>
</beans>
American类
/**
*
*/
/**
* @author c
*
*/
package tju.chc.app.service.impl;
import java.util.List;
import java.util.Map;
import java.util.Set;
import tju.chc.app.service.Axe;
import tju.chc.app.service.Person;
public class American implements Person{
private Axe axe;
private String name;
private int age;
private List schools;
private Map scores;
private Set axes;
//设置注入所需的setter方法
public void setAxe(Axe axe) {
this.axe = axe;
}
//设置注入所需的setter方法
public void setName(String name) {
this.name = name;
}
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;
}
public void useAxe() {
//调用axe的chop()方法
//表明Person对象依赖于axe对象
System.out.println("我是:"+name+","+axe.chop());
}
}
测试例子
package qi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tju.chc.app.service.impl.American;
public class BeanTest_util {
public static void main(String[] args){
ApplicationContext cxt = new ClassPathXmlApplicationContext("bean_p_util.xml");
American ame = cxt.getBean("american", American.class);
ame.useAxe();
System.out.println(ame.getAge());
System.out.println(ame.getAxes());
System.out.println(ame.getSchools());
System.out.println(ame.getScores());
}
}
测试结果
我是:孙悟空,石斧砍柴好慢
8
[tju.chc.app.service.impl.StoneAxe@496396ae, tju.chc.app.service.impl.SteelAxe@e3923e5]
[小学, 中学, 大学]
{数学=87, 英语=87, 语文=800}
除此之外,关于Spring其它常用的简化Schema的
spring-aop-3.0.xsd :用于简化SPring AOP配置的Schema
spring-jee-3.0.xsd :用于简化SPring的Java EE配置的Schema
spring-jms-3.0.xsd :用于简化spring关于JMS配置的Schema
spring-lang-3.0.xsd :用于简化SPring动态语言配置的Schema
spring-tx-3.0.xsd :用于简化Spring事务配置的Schema