您的位置:首页 > 其它

使用XML Schema的简化配置方式

2016-02-29 23:11 435 查看

1.使用P:命名空间

p:命名空间甚至不需要特定的Schema定义,它直接存在于Spring内核中。与前面采用<property…/>元素定义Bean的属性不同是,当导入p:命名空间之后,就可以直接在<bean…/>元素中使用属性来驱动执行setter方法。

下面是一个持久化的类

public class Chinese implements Person
{
private Axe axe;
private int age;
public Chinese(){ }
// axe的setter方法
public void setAxe(Axe axe)
{
this.axe = axe;
}
// age的setter方法
public void setAge(int age)
{
this.age = age;
}
// 实现Person接口的useAxe()方法
public void useAxe()
{
System.out.println(axe.chop());
System.out.println("age成员变量的值:" + age);
}
}


上面的持久化类在配置文件的配置。

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的根元素和Schema
并导入p:命名空间的元素 -->
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 配置chinese实例,其实现类是Chinese -->
<bean id="chinese" class="com.owen.app.service.impl.Chinese"
p:age="29" p:axe-ref="stoneAxe"/>
<!-- 配置steelAxe实例,其实现类是SteelAxe -->
<bean id="steelAxe" class="com.owen.app.service.impl.SteelAxe"/>
<!-- 配置stoneAxe实例,其实现类是StoneAxe -->
<bean id="stoneAxe" class="com.owen.app.service.impl.StoneAxe"/>
</beans>

配置文件中需要导入XML Schema里的p:命名空间(xmlns:p="http://www.springframework.org/schema/p)。在代码中直接使用属性配置age、axe执行设值注入,因为axe设值注入的参数需要引用容器中另一个已经存在的Bean实例,故在axe后增加了“-ref”后缀,这个后缀指定该值不是一个具体的值,而是对另外一个Bean的引用。

2.使用c:命名空间

p:命名空间主要用于简化设值注入,而c:命名空间则用于简化构造注入。

下面是一个持久化的类。

public class Chinese implements Person
{
private Axe axe;
private int age;
// 构造注入所需的带参数的构造器
public Chinese(Axe axe, int age)
{
this.axe = axe;
this.age = age;
}
// 实现Person接口的useAxe()方法
public void useAxe()
{
// 调用axe的chop()方法
// 表明Person对象依赖于axe对象
System.out.println(axe.chop());
System.out.println("age成员变量的值:" + age);
}
}

上面的配置方式是在c:后使用构造器参数名来指定构造参数,Spring还支持一种通过索引来配置构造器参数的方式。上面的Bean也可改写为如下:

<bean id=”chinese2”class=”com.owen.app.service.impl.Chinese” c:_0-ref=”steelAxe” c_1=”29”/>

这种方式下,c:_N中的N代表第几个构造参数。

3.使用util:命名空间

在Spring构架解压缩包的schema\util路径下包含有util:命名空间的XMLSchema文件,为了使用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: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">

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

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

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

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

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

5) map:该元素用于定义一个MapBean,支持使用<entry…/>来定义Map的key-value对。

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

下面定义一个Bean类,这个类中包含List、Set、Map等集合。

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);
}
}

下面定义使用基于XML Schema的配置文件。

 <?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="com.owen.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="com.owen.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="com.owen.app.service.impl.SteelAxe"/>
<!-- 配置stoneAxe实例,其实现类是StoneAxe -->
<bean id="stoneAxe" class="com.owen.app.service.impl.StoneAxe"/>
</beans>

4.总结

Spring提出的这种XML Schema的配置方式,可以对Spring配置文件进行缩小,这样配置出来的文件会简单、直观,而且能以相同风格处理所有Bean的配置——唯一的缺点是配置烦琐。当Bean实例的属性足够多,且属性类型复杂时,基于DTD的配置文件将变得更加烦琐。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: