1 SpringXml配置模板如下
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3/org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context">
</beans>
2 声明一个简单的bean
<bean class="soundsystem.SgtPeppers" />
在本例中,bean的ID将会是“soundsystem.SgtPeppers#0”。其 中,“#0”是一个计数的形式,用来区分相同类型的其他bean。如果你声明了另外一个SgtPeppers,并且没有明确进行标识,那么它自动得到 的ID将会是“soundsystem.SgtPeppers#1”。
<bean id="compactDisc" class="soundsystem.SgtPeppers" />
3 借助构造器注入初始化bean
- 将引用注入到bean中
<bean id="cdPlayer" class="soundsystem.CDplayer">
<constructor-arg ref="compactDisc" />
</bean>
替代方案,也可以使用spring的c命名空间,但是必须要在xml的顶部声明其模式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http"//www.springframewotrk.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:xsi="http"//www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:www.springframe.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context">
<beans>
加入c命名空间后,可以使用以下写法:
<bean id="cdPlayer" class="soundsystem.CDPlayer" c:cd-ref="compactDisc" />
其中c:cd-ref中,c是命名空间前缀,cd构造器参数名,ref注入bean引用,compactDisc是引用bean的id
另一种写法:
<bean id="cdPlayer" class="soundsystem.CDPlayer" c:_0-ref="compactDisc" />
其中0是参数的索引,在构造器中的位置.如果构造器只有一个参数,则可以简化成如下写法:
<bean id="cdPlayer" class="soundsystem.CDPlayer" c:_-ref="compactDisc" />
- 将具体的值注入到bean中:
package soundsystem
public class BlankDisc implements CompactDisc {
private String title;
private String artist;
public BlankDisc(String title,String artist){
this.title = title;
this.artist = artist;
}
public void play(){
System.out.println("Playing " + title + " by " + artist);
}
}
xml配置
常规写法:
<bean id="compactDisc" class="soundsysyem.BlankDisc">
<constructor-arg value="sgt. pepper's Lonely Heats Club Band" />
<constructor-arg value="The Beatles" />
</bean>
使用c命名空间:
<bean id="compactDisc" class="soundsystem.BlankDisc" c:_title="sgt. pepper's Lonely Heats Club Band" c:_artist="The Beatles" />
简化写法:
<bean id="compactDisc" class="soundsystem.BlankDisc" c:_0="sgt. pepper's Lonely Heats Club Band" c:_1="The Beatles" />
如果构造器只有一个参数时,则可以省略参数索引
<bean id="compactDisc" class="soundsystem.BlankDisc" c:_="sgt.pepper's Lonely Heats Club Band" />
- 装配集合
package soundsystem
public class BlankDisc implements CompactDisc {
private String title;
private String artist;
private String List<String> tracks;
public BlankDisc(String title,String artist, List<String> tracks){
this.title = title;
this.artist = artist;
this.tracks = tracks;
}
public void play(){
System.out.println("Playing " + title + " by " + artist);
}
}
常规配置
<bean id="compactDisc" class="soundsysyem.BlankDisc">
<constructor-arg value="sgt. pepper's Lonely Heats Club Band" />
<constructor-arg value="The Beatles" />
<constructor-arg><null/></constructor>
</bean>
此时,tracks的值是null
<bean id="compactDisc" class="soundsysyem.BlankDisc">
<constructor-arg value="sgt. pepper's Lonely Heats Club Band" />
<constructor-arg value="The Beatles" />
<constructor-arg>
<list>
<value>sgt.Pepper's Lonely Hearts Club Band></value>
<value>With a Little Help from My Friends</value>
<value>Getting Better</value>
</list>
</constructor-arg>
</bean>
类似的也可以使用ref代替value
<bean id="compactDisc" class="soundsysyem.BlankDisc">
<constructor-arg value="sgt. pepper's Lonely Heats Club Band" />
<constructor-arg value="The Beatles" />
<constructor-arg>
<list>
<ref bean="sgtPeppers" />
<ref bean="whiteAlbum" />
<ref bean="hardDaysNight"/>
</list>
</constructor-arg>
</bean>
上述配置中也可以将<list>元素换为<set>,两者的区别不大,如果bean中的属性是Set时,所有重复的值都会被忽略掉,存放的顺序也得不到保证.但是不管在什么情况下,<list>,<set>都可以用来装配List,Set以及数组.
在装配集合时<constructor-arg>比c命名空间更有优势.目前,使用c命名空间的属性无法实现装配集合的功能.
- 设置属性
package soundsystem;
import org.springframework.beans.factory.annotation.Autowired;
import soundsystem.CompactDisc;
import soundsystem.MediaPlayer;
public class CDPlayer implements MediaPlayer {
private CompactDisc compactDisc;
@AutoWired
public void setCompactDisc(CompactDisc compactDisc){
this.compactDisc = compactDisc;
}
public void play(){
compactDisc.play();
}
}
此时CDPlayer只有默认的构造器,也没有其他强依赖,如果使用如下方式将其声明为spring bean时,那么调用paly方法时将会报空指针异常
<bean id="cdPlayer" class="soundsystem.CDPlayer"/>
解决方案:可以用property元素为属性的setter方法提供注入功能
<bean id="cdPlayer" class="soundsystem.CDPlayer">
<property name="compactDisc" ref="compactDisc" />
</bean>
同样的property元素也具有p命名空间,在xml文件中与其他的命名空间一起对其声明:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframe.org/schema/p"
xmlns:xsi="http"//www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</bean>
使用p命名空降声明bean
<bean id="cdPlayer" class="soundsystem.CDPlayer" p:compactDisc-ref="compactDisc" />
其中[p:compactDisc-ref],p表示p命名空间,compactDisc是属性名,-ref:注入属性的引用
5) 将字面量注入到属性中
package soundsystem;
import java.util.List;
import soundsystem.CompactDisc;
public class BlankDisc implements CompcatDisc{
private String title;
private String artist;
private List<String> tracks;
public void setTitle(String title){
this.title = title;
}
public void setArtist(String artist){
this.artist = artist;
}
public void setTracks(List<String> tracks){
this.tracks = tracks;
}
public void play(){
Systeml.out.println("Playing " + title +" by " +artist);
for(String track:tracks){
System.out.println("-Track: " + track);
}
}
}
xml配置:
<bean id="compactDics" class="soundsystem.BlankDisc">
<property name="title" value="Sgt.Pepper's Lonely Hearts Club Band" />
<property name="artist" value="The Beatles" />
<propertyname="tracks">
<list>
<value>Sgt.Pepper's Lonely Hearts Club Band</value>
<value>With a Little Help from my friends</valuey>
</list>
</propert>
</bean>
p命名空间简化写法
<bean id="compactDics" class="soundsystem.BlankDisc" p:title="Sgt.Pepper's Lonely Hearts Club Band" p:artist="The Beatles">
<propertyname="tracks">
<list>
<value>Sgt.Pepper's Lonely Hearts Club Band</value>
<value>With a Little Help from my friends</valuey>
</list>
</propert>
</bean>
装配bean引用与装配字面量的唯一区别在于"-ref"后缀.
注意:p-命名空间不能用来装配集合,没有便利的方式使用p-命名空间来指定一个值(或bean的引用)的列表.但是我们可以使用Spring util-命名空间的一些功能来简化BlankDiscbean.首先需要在xml中声明util-命名空间及其模式:
<!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.springframewotrk.org/schema/util/spring-util.xsd">
</beans>
util-命名空间的功能之一就是util:list元素,他会创建一个列表的bena.借助util:list.如下:
<util:list id="trackList">
<value>Sgt.Pepper's Lonely Hearts Club Band</value>
<value>With a Little Help from my friends</valuey>
</util:list>
<bean id="compactDisc" class="soundsystem.BlankDisc" p:title="Sgt.Pepper's Lonely Hearts Club Band" p:artist="The Beatles" p:tracks-ref="trackList">
util-命名空间提供的元素有:
<util:constant>:引用某个类型的public static 域,并将其暴露为bean
<util:list> 创建一个java.util.List类型的bean,其中包含值或引用
<util:map> 创建一个java.util.Map类型的bean,其中包含值或引用
<util:properties> 创建一个java.util.pproperties类型的bean
<util:property-path>引用一个bean的属性(或内嵌属性),并将其暴露为bean
<util:set>创建一个java.util.Set类型的bean,其中包含值或引用