1、使用 p 名称空间配置属性
p 名称空间甚至不需要特定的Schema定义,它直接存在于Spring内核中。与前面采用<property.../>元素定义Bean的属性不同的是,当我们采用了 p 名称空间之后,就可直接在<bean.../>元素中使用属性来定义Bean实例的属性值了。
package SimplifiedConfigurationMode;
public class Chinese implements Person
{
private Axe axe;
private int age;
public void setAxe(Axe axe) {
this.axe = axe;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void useAxe() {
// TODO Auto-generated method stub
System.out.println(axe.chop());
System.out.println("age属性值:"+age);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<!-- 指定Spring配置文件的根元素和Schema 并导入p命名空间的元素 -->
<bean id="chinese" class="SimplifiedConfigurationMode.Chinese"
p:age="20" p:axe-ref="stoneAxe"/>
<bean id="stoneAxe" class="DependencyInjection.StoneAxe"/>
</beans>
配置的属性中,因为axe属性需要引用容器中另一个已存在的Bean实例,故在axe后增加了“-ref”后缀,这个后缀指定该值不是一个具体的值,而是对另外一个Bean的引用。
package SimplifiedConfigurationMode;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import DependencyInjection.Person;
public class Test {
public static void main(String[] args) {
//创建Spring容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("namespace.xml");
//获取chinese实例
Person p = ctx.getBean("chinese",Person.class);
p.useAxe();
}
}
PS:使用 p 名称空间没有标准的XML格式灵活,如果某个Bean的属性名是以“-ref”结尾的,那么采用 p名称空间定义时就会导致冲突,而采用标准XML格式定义则不会出现这种问题。
2、使用util Schema
在Spring框架解压缩包包含大量Schema文件,例如spring-beans-*.*.xsd、spring-tools-*.*.xsd和spring-context-*.*.xsd等等,在这些Schema中,只有spring-beans-*.*.xsd是Spring的内核,其他Schema大都用于简化某些方面的配置。此处介绍的是spring-util-*.*.xsd Schema的简化功能。
package SimplifiedConfigurationMode;
import java.util.*;
public class SetValueInjection implements Person {
private Axe axe;
private int age;
private List schools;
private Map scores;
private Set axes;
@Override
public void useAxe() {
// TODO Auto-generated method stub
System.out.println(axe.chop());
System.out.println("age属性值:"+age);
System.out.println("List类型属性值:"+schools);
System.out.println("Map类型属性值:"+scores);
System.out.println("Set类型属性值:"+axes);
}
public void setAxe(Axe axe) {
this.axe = axe;
}
public void setAge(int age) {
this.age = age;
}
public void setSchools(List schools) {
this.schools = schools;
}
public void setScores(Map scores) {
this.scores = scores;
}
public void setAxes(Set axes) {
this.axes = axes;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 指定Spring配置文件的根元素和Schema 并导入p命名空间和util命名空间的元素 -->
<bean id="stoneAxe" class="DependencyInjection.StoneAxe"/>
<bean id="setValueInjection" class="SimplifiedConfigurationMode.SetValueInjection"
p:axe-ref="stoneAxe" p:age-ref="s.age"
p:schools-ref="s.schools" p:scores-ref="s.scores"
p:axes-ref="s.axes"/>
<!-- 使用util:constant 将指定类的静态Field暴露出来 -->
<util:constant id="s.age" static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
<!-- 使用util:property-path 将指定Bean的指定属性暴露出来 -->
<util:property-path id="test" path="setValueInjection.age"/>
<!-- 使用util:list 定义一个List对象 -->
<util:list id="s.schools" list-class="java.util.LinkedList">
<value>小学</value>
<value>中学</value>
<value>大学</value>
</util:list>
<!-- 使用util:set 定义一个Set对象 -->
<util:set id="s.axes" set-class="java.util.HashSet">
<!-- 每个value、ref、bean都配置一个Set元素 -->
<value>字符串斧子</value>
<bean class="DependencyInjection.SteelAxe"/>
<ref bean="stoneAxe"/>
</util:set>
<!-- 使用util:map 定义一个Map对象 -->
<util:map id="s.scores" map-class="java.util.TreeMap">
<entry key="数学" value="87"/>
<entry key="语文" value="89"/>
<entry key="英语" value="82"/>
</util:map>
</beans>
package SimplifiedConfigurationMode;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import DependencyInjection.Person;
public class Test {
public static void main(String[] args) {
//创建Spring容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("namespace.xml");
Person p = ctx.getBean("setValueInjection",Person.class);
p.useAxe();
}
}
从上面的配置文件可以看出,使用这种简化标签确实可让Spring配置文件更加简洁;但相对而言,这种配置方式降低了Spring配置文件的可读性。
除了spring-util-*.*.xsd之外,关于Spring其他常用的简化Schema的简要说明如下:
> spring-aop-*.*.xsd:用于简化Spring AOP配置的Schema。
> spring-jee-*.*.xsd:用于简化Spring 的Jave EE配置的Schema。
> spring-jms-*.*.xsd:用于简化Spring 关于JMS配置的Schema。
> spring-lang-*.*.xsd:用于简化Spring 动态语言配置的Schema。
> spring-tx-*.*.xsd:用于简化Spring 事务配置的Schema。