Spring 装配Bean(三):通过XML装配bean
创建XML配置规范
使用XML文件配置Spring时,首先创建一个xml文件,并且要以<beans>元素为根
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--your configuration go here-->
</beans>
声明一个简单的bean
<bean id="compactDisc" class="soundsystem.sgtPeppers">
</bean>
如果不适用id属性为bean设置ID,那么默认的命名为全限定的类名加一个序列号,比如“soundsystem.SgtPeppers#0”
,如果相同类型的bean不止一个,就可以通过序列号对其进行区分。
<注意>:因为bean的类型是以字符串的形式设置给class属性的,所以xml文件并不能保证这个字符串对应了一个真正的class,所以编译时就不能检查出XML配置是否出错,但是有的IDE可以检查xml的合法性,如Spring Tool Suite。
借用构造器注入初始化bean
声明bean时使用<constructor-arg>
元素指定要注入的构造器参数,ref属性通过ID引用上面的bean就可以了
<bean id="CDPlayer" class="soundsystem.CDPlayer" >
<constructor-arg ref="compactDisc" />
</bean>
这种方式需要在XML顶部声明使用c-命名空间的模式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--your configuration go here-->
</beans>
声明之后,就可以来声明构造器参数,下面的方式结果等同于使用<constructor-arg>
指定构造器参数:
<bean id="CDPlayer" class="soundsystem.CDPlayer"
c:cd-ref="compactDisc" />
其中cd-ref部分cd是构造器参数名,ref表示后面的变量是bean的引用
如果要注入的是参数的值而非引用,那么上述两种形式的改变为(如果BlankDisc构造器有两个参数title以及artist均为字符串类型)
使用value属性而不是ref
<bean id="BlankDisc" class="soundsystem.BlankDisc" >
<constructor-arg value="Sgt.Peppers Lonely Hearts Club Band" />
<constructor-arg value="The Beatles" />
</bean>
c命名空间去掉-ref
<bean id="BlankDisc" class="soundsystem.BlankDisc"
c:_title="Sgt.Peppers Lonely Hearts Club Band"
c:_artists="The Beatles" />
也可以通过索引装配,0,1指明构造器中参数位置索引,如果只有一个参数甚至可以只用一个下划线c:_="xxxx"
<bean id="BlankDisc" class="soundsystem.BlankDisc"
c:_0="Sgt.Peppers Lonely Hearts Club Band"
c:_1="The Beatles" />
装配集合
之前我们使用bean进行装配的都是简单的java对象引用或者字面量,如果构造器参数中含有集合对象时,应该如何装配呢。例如下面的CD类中有一个属性为tracks的列表来描述CD中的磁道:
package soundSystem;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class BlankDisc implements CompactDisc {
private String title;
private String artist;
private 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);
for (String track : tracks) {
System.out.println("-Track: " + track);
}
}
}
此时我们在XML文件中需要使用<list>
元素以及<value>
元素从构造器中注入列表对象。
<bean id="blankDisc" class="soundSystem.BlankDisc" >
<constructor-arg value="Sgt.Peppers Lonely Hearts Club Band" />
<constructor-arg value="The Beatles" />
<constructor-arg>
<list>
<value>Sgt.Peppers Lonely Hearts Club Band</value>
<value>With a Little Help From My Friend</value>
<value>Lucy in the Sky with Diamonds</value>
<value>Getting Better</value>
<value>Fixing a hole</value>
</list>
</constructor-arg>
</bean>
如果列表中的元素不是值而是bean的引用当然也可以使用<ref>
元素.和<list>
元素类似的还有<set>
元素,这两者的区别就在于在实际的类中的属性是java.util.list还是java.util.set,Set元素中重复的值会被忽略掉,而且元素是无序的。
设置属性(setter注入)
如果属性注入的CDplayer如下
package soundSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class CDPlayer implements MediaPlayer{
private SgtPeppers cd;
public CDPlayer (SgtPeppers cd){
this.cd = cd;
}
/*setter注入*/
@Autowired
public void setCompactDisc(SgtPeppers cd){
this.cd = cd;
}
@Override
public void play(){
cd.play();
}
}
在XML文件中就可以使用<property>
属性来注入SgtPeppers对象
<bean id="cdPlayer" class="soundsystem.CDPlayer" >
<property name="cd" ref="sgtPeppers" />
</bean>
类似于c命名空间,设置属性时也可以使用p命名空间,首先需要在XML文件中声明使用p命名空间:
<?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: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
https://www.springframework.org/schema/context/spring-context.xsd">
使用 p命名空间,xml中bean的setter注入就可以表示成这样:
<bean id="cdPlayer" class="soundsystem.CDPlayer"
p:cd-ref="sgtPeppers"/>
同样,在<property>
元素中我们还可以使用<list>
元素和<set>
元素来引用集合元素。
util-命名空间
由于c-命名空间以及p-命名空间中都不好引用集合元素,由此出现了util-命名空间,他可以直接把集合声明为bean,比如上述CD中的磁道列表我们可以把它用util-命名空间创建为一个bean:
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
https://www.springframework.org/schema/util/spring-util.xsd">
<util:list id = "trackList">
<value>Sgt.Peppers Lonely Hearts Club Band</value>
<value>With a Little Help From My Friend</value>
<value>Lucy in the Sky with Diamonds</value>
<value>Getting Better</value>
<value>Fixing a hole</value>
</util:list>
<bean id="compactDisc" class="soundsystem.CompactDisc"
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:property> | 创建一个java.util.Property类型的bean,其中包含值或者引用 |
<util:property-path> | 引用一个bean的属性(或者内嵌属性),并将其暴露为bean |
<util:set> | 创建一个java.util.Set类型的bean,其中包含值或者引用 |