对强依赖使用构造器注入,对可选性依赖使用属性注入
c和p都是在最后闭合>并且都带-ref
装配bean引用和字面量区别在于是不是带-ref 字面量带value,有-ref为bean引用
最为简单的Spring XML配置如下
<?xml version="1.0" encoding="UTF-8">
<bean xmlns="http://www.springframework.org/scheme/beans"
xmlns:xsi="http://www.w3.org/2001/XMLScheme-instance"
xsi:schemeLocation="http://www.springframework.org/scheme/beans
http://www.springframework.org/scheme/beans/spring-beans.xsd
http://www.springframework.org/scheme/context ">
<!-- configuration details go here-->
</bean>
构造器装配将构造器的引用装配到其他对象中
<constructor-arg>
<bean id="cdplayer" class="soudsystem.CDPlayer">
<constructor-arg ref="compactDisc">
ref里的值就是构造函数里的参数 告知Spring要将一个ID为compactDisc的bean引用传递到CDPlayer的构造器中
2.Spring3.0 的c-命名空间
先在XML顶部说明其模式
<?xml version="1.0" encoding="UTF-8">
<bean xmlns="http://www.springframework.org/scheme/beans"
xmlns:c="http://www.springframework.org/scheme/c"
xmlns:xsi="http://www.w3.org/2001/XMLScheme-instance"
xsi:schemeLocation="http://www.springframework.org/scheme/beans
http://www.springframework.org/scheme/beans/spring-beans.xsd
http://www.springframework.org/scheme/context ">
<!-- configuration details go here-->
</bean>
<bean id="cdplayer" class="soundsystem.cdplayer"
c:cd-ref="compactDisc"/>
-ref告诉Spring正在装配的是一个bean引用而不是字面量
为了避免直接使用构造器参数的名称,可以使用参数在整个参数列表中的位置信息
c:_0-ref="compactDisc"
XML不允许数字作为属性的第一个字符,所以要嫁一个下划线作为前缀
如果只有一个构造器参数 可以简化为 c:_-ref="compactDisc"
将字面量注入到构造器中
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("title:"+tile+"artist:"+artist);
}
}
<bean id="compactDisc" class="soundsystem.BlankDisc">
<constructor-arg value="lonely hearts club band"/>
<constructor-arg value="The Beatles"/>
</bean>
<bean id="compactDisc" class="soundsystem.BlankDisc">
<c:_title="lonely hearts club band"/>
<c:_artist="The Beatles"/>
</bean>
类似的
<bean id="compactDisc" class="soundsystem.BlankDisc">
<c:_0="lonely hearts club band"/>
<c:_1="The Beatles"/>
</bean>
装配集合
当有一个属性为集合类型时,只能够使用<constructor-arg>
装配
例如加入一个 private List<String> tracks
;
装配为
<constructor-arg>
<list>
<value>Getting Better</value>
<value>Fixing a Hole</value>
<value>Lucy in the Sky wIth Diamonds</value>
<!--other tracks-->
</list>
</constructor-arg>
此处的list也可以换成set 区别就是所有重复的值会被忽略掉,存放位置也随便
属性注入
当对象中只有set方法时,使用property进行属性注入
@Autowired
public void setCompactDisc(CompactDisc compactDisc) {
this.compactDisc=compactDisc;
}
<bean id="cdPlayer" class="soundsystemCDPlayer">
<property name="compactDisc" ref="compactDisc"/>
</bean>
与c-命名空间类似也有p-命名空间
相应的XML配置
<?xml version="1.0" encoding="UTF-8">
<bean xmlns="http://www.springframework.org/scheme/beans"
xmlns:p="http://www.springframework.org/scheme/p"
xmlns:xsi="http://www.w3.org/2001/XMLScheme-instance"
xsi:schemeLocation="http://www.springframework.org/scheme/beans
http://www.springframework.org/scheme/beans/spring-beans.xsd
http://www.springframework.org/scheme/context ">
<!-- configuration details go here-->
</bean>
<bean id="cdPlayer" class="soundsystem.CDPlayer"
p:compactDisc-ref="compactDisc"/>
p-命名空间不能装配集合可以使用Spring util-命名空间来简化
相应的XML配置
<?xml version="1.0" encoding="UTF-8">
<bean xmlns="http://www.springframework.org/scheme/beans"
xmlns:p="http://www.springframework.org/scheme/p"
xmlns:util="http://www.springframework.org/scheme/util"
xmlns:xsi="http://www.w3.org/2001/XMLScheme-instance"
xsi:schemeLocation="http://www.springframework.org/scheme/beans
http://www.springframework.org/scheme/beans/spring-beans.xsd
http://www.springframework.org/scheme/context ">
<!-- configuration details go here-->
</bean>
<util-list id="tracklist">
<value>Getting Better</value>
<value>Fixing a Hole</value>
<value>Lucy in the Sky wIth Diamonds</value>
<!--other tracks-->
</util-list>
再将这个列表注入到BlankDisc bean的track属性中
<bean id="compactDisc" class="soundsystem.BlankDisc"
p:title="lonely hearts club band"
p:artist="The Beatles"
p:tracks-ref="tracklist"/>