使用p:命名空間簡(jiǎn)化配置:
Chinese.java
package entity;
import inter.Axe;
import inter.Persion;
public class Chinese implements Persion{
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);
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的根元素和Schema并導(dǎo)入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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
<!-- 使用p:命名空間配置Bean -->
<bean id="chinese" class="entity.Chinese" p:age="29" p:axe-ref="steelAxe"/>
<bean id="steelAxe" class="entity.SteelAxe" />
</beans>
使用c:命名空間簡(jiǎn)化配置:
p:命名空間主要用于簡(jiǎn)化設(shè)值注入,而c:命名空間主要用于簡(jiǎn)化構(gòu)造注入。
使用c:指定構(gòu)造參數(shù)的格式為:c:構(gòu)造器參數(shù)名="值"或c:構(gòu)造器參數(shù)名-ref="其他Bean的id"
Chinese.java
package entity;
import inter.Axe;
import inter.Persion;
public class Chinese implements Persion{
private Axe axe;
private int age;
public Chinese(Axe axe,int age)
{
this.axe=axe;
this.age=age;
}
@Override
public void useAxe() {
// TODO Auto-generated method stub
System.out.println(axe.chop());
System.out.println("age成員變量的值:"+age);
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的根元素和Schema并導(dǎo)入c:命名空間 -->
<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
<!-- 使用p:命名空間配置Bean -->
<bean id="chinese" class="entity.Chinese" c:axe-ref="steelAxe" c:age="29"/>
<bean id="steelAxe" class="entity.SteelAxe" />
</beans>
Spring還支持一種通過(guò)索引來(lái)配置構(gòu)造參數(shù)的方式。
<bean id="chinese" class="entity.Chinese" c:_0-ref="steelAxe" c:_1="29"/>
使用util:命名空間簡(jiǎn)化配置:
為了使用util:命名空間的元素,必須先再Spring配置文件中導(dǎo)入最新的spring-util-4.0.xsd
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的根元素和Schema并導(dǎo)入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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd >
</beans>
util Schema下提供了如下幾個(gè)元素:
- constant:該元素用于獲取指定類的靜態(tài)Field的值。
- property-path:該元素用于獲取指定對(duì)象的getter方法的返回值。
- list:該元素用于定義一個(gè)List Bean,支持使用<value.../>,<ref.../>,<bean.../>等子元素來(lái)定義LIst集合元素。
- set:該元素用于定義一個(gè)Set Bean,支持使用<value.../>,<ref.../>,<bean.../>等子元素來(lái)定義Set集合元素。
- map:該元素用于定義一個(gè)Map Bean,支持使用<value.../>,<ref.../>,<bean.../>等子元素來(lái)定義Map集合元素。
- properties:該元素用于加載一份資源文件,并根據(jù)加載的資源文件創(chuàng)建一個(gè)properties Bean實(shí)例。