1、使用p:命名空间简化配置
用途:简化<property.../>配置,使用后可直接在<bean.../>元素中定义
需引入 xmlns:p="http://www.springframework.org/schema/p"
使用:
定义两个接口:
public interface Person {
void fire();
}
public interface Gun {
void shoot();
}
定义类分别实现两个接口
public class AramPerson implements Person {
private Gun gun;
private String who;
public void setGun(Gun gun) {
this.gun = gun;
}
public void setWho(String who) {
this.who = who;
}
@Override
public void fire() {
gun.shoot();
System.out.println("这是"+who);
}
}
public class RefeGun implements Gun {
@Override
public void shoot() {
System.out.println("来复枪射倒一大片");
}
}
public class MechineGun implements Gun {
@Override
public void shoot() {
System.out.println("机关枪火力压制");
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="refegun" class="simple.RefeGun"/>
<bean id="mechinegun" class="simple.MechineGun"/>
<!--简化配置各项Bean-->
<bean id="aram" class="simple.AramPerson" p:gun-ref="refegun" p:who="下士"/>
<bean id="aram2" class="simple.AramPerson" p:gun-ref="mechinegun" p:who="中士"/>
</beans>
注意上述配置文件引入了:
xmlns:p="http://www.springframework.org/schema/p"
测试:
public class Test {
public static void main(String[] args) throws Exception{
ApplicationContext appcon = new ClassPathXmlApplicationContext("applicationContext4.xml");
simple.service.Person p1 = appcon.getBean("aram",simple.service.Person.class);
p1.fire();
simple.service.Person p2 = appcon.getBean("aram2",simple.service.Person.class);
p2.fire();
}
}
测试截图:
2、使用c:命名空间简化配置
用途:简化构造注入
引入:xmlns:c="http://www.springframework.org/schema/c"
将上述实现类AramPerson.java添加构造方法,其他不变
public AramPerson(Gun gun,String who){
this.gun = gun;
this.who = who;
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="refegun" class="simple.RefeGun"/>
<bean id="mechinegun" class="simple.MechineGun"/>
<!--c:命名空间简化配置-->
<bean id="aram3" class="simple.AramPerson" c:gun-ref="refegun" c:who="第一队"/>
<bean id="aram4" class="simple.AramPerson" c:gun-ref="mechinegun" c:who="第二队"/>
</beans>
测试截图:
Spring还支持通过索引配置,将上述配置改为如下方式是一样的效果:
<bean id="aram3" class="simple.AramPerson" c:_0-ref="refegun" c:_1="第一队"/>
<bean id="aram4" class="simple.AramPerson" c:_0-ref="mechinegun" c:_1="第二队"/>
3、使用util:命名空间简化配置
为了使用util命名空间,必须先在Spring配置文件中导入最新的spring-util.xsd,
如下配置截图:
util Schema提供元素介绍:
- constant:获取指定类的静态Field值,是FieldRetrievingFactoryBean的简化配置
- property-path:获取指定对象的getter方法的返回值,是PropertyPathFactoryBean的简化配置
- list:定义一个List Bean,支持使用<value../>、<ref.../>、<bean.../>等子元素来定义list集合元素,该元素支持下面三个属性
id:为该元素指定一个名为id的List Bean实例
list-class:该属性指定使用哪个List实现类来创建Bean实例,默认ArrayList
scope:指定List Bean实例的作用域
- set:定义一个Set Bean,支持使用<value../>、<ref.../>、<bean.../>等子元素来定义list集合元素,该元素支持下面三个属性。
id:为该元素指定一个名为id的Set Bean实例
set-class:该属性指定使用哪个Set实现类来创建Bean实例,默认HashSet
scope:指定Set Bean实例的作用域
- map:该元素用于定义一个Map Bean,支持使用<entry.../>来定义Map的key-value对,支持下面三个属性
id:为该元素指定一个名为id的Map Bean实例
map-class:该属性指定使用哪个Map实现类来创建Bean实例,默认HashMap
scope:指定Map Bean实例的作用域
- properties:该元素用于加载一份资源文件,并根据所加载的资源文件创建一个Properties Bean实例,可指定如下几个属性。
id:定义一个名为id的Properties Bean实例
location:该属性指定资源文件的文职
scope:指定该Properties Bean实例的作用域
代码:为实现类增加属性
public class AramPerson implements Person {
private Gun gun;
private String who;
private List schools;
private Map scores;
private Set guns;
.....省略getter和setter方法
@Override
public void fire() {
gun.shoot();
System.out.println("这是"+who);
System.out.println(schools);
System.out.println(scores);
System.out.println(guns);
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="refegun" class="simple.RefeGun"/>
<bean id="mechinegun" class="simple.MechineGun"/>
<!--配置Person实例-->
<bean id="person" class="simple.AramPerson"
p:gun-ref="mechinegun"
p:who-ref="per.who"
p:schools-ref="per.school"
p:scores-ref="per.scores"
p:guns-ref="per.guns"
/>
<!--使用util命名空间配置相关Bean-->
<util:constant id="per.who" static-field="java.sql.Connection.TRANSACTION_NONE" />
<util:properties id="conftest" location="classpath:message_zh_CN.properties"/>
<util:list id="per.school">
<value>小学</value>
<value>中学</value>
<value>大学</value>
</util:list>
<util:set id="per.guns">
<value>字符串</value>
<bean class="simple.MechineGun"/>
<ref bean="refegun"/>
</util:set>
<util:map id="per.scores">
<entry key="数学" value="70"/>
<entry key="语文" value="71"/>
<entry key="英语" value="72"/>
</util:map>
</beans>
测试截图:
4、其他简化配置简单介绍
- Spring-aop.xsd:简化Spring AOP配置
- Spring-jee.xsd:简化Spring的Java EE配置
- Spring-jms.xsd:简化Spring关于JMSde 配置
- Spring-lang.xsd:简化Spring动态语言配置
- Spring-tx.xsd:简化Spring事务配置