Spring可以借助属性的set方法来配置属性的值,以实现setter方式的注入。
在Spring中我们可以使用<\property>元素配置Bean的属性。<\property>元素为属性的Setting方法所提供的功能与<\constructor-arg>元素为构造器所提供的功能是一样的。只不过一个是通过构造参数来注入值,另一个是通过调用属性的setter方法来注入值。
一、注入字面量值
首先使用<\property>为字面量注入值:
public void setBeanbags(int beanbags) {
this.beanbags = beanbags;
}
public void setName(String name) {
this.name = name;
}
<bean id="duke" class="org.springaction.Juggler">
<!--value属性既可以注入String类型的值,也可以注入数值型(int,float,Double)以及boolean型的值-->
<property name="beanbags" value="21"/>
<property name="name" value="what fuxk"/>
</bean>
可以使用p-命名空间来完成注入,需要在XML顶部加入声明:
xmlns:p="http://www.springframework.org/schema/p"
修改之后:
<!--利用p命名空间将多个字面量注入到属性-->
<bean id="duke" class="org.springaction.Juggler"
p:beanbags="32"
p:name="whis is"/>
二、将其他bean注入到属性
使用<\property>为bean注入值:
public void setCd(CompactDisc cd) {
this.cd = cd;
}
public void setCdPlat(CDPlat cdPlat) {
this.cdPlat = cdPlat;
}
<bean id="duke" class="org.springaction.Juggler">
<property name="cd" ref="sgtPeppers"/>
<property name="cdPlat" ref="cdPlat"/>
</bean>
<bean id="sgtPeppers" class="org.springaction.SgtPeppers"/>
<bean id="cdPlat" class="org.springaction.CDPlat"/>
使用p-命名空间对其进行修改:
<bean id="duke" class="org.springaction.Juggler"
p:cd-ref="sgtPeppers"
p:cdPlat-ref="cdPlat"/>
对p-命名空间的说明:首先属性的名字使用”p:”前缀,接下来就是要注入的属性名,最后,属性以”-ref”结尾,提示Spring要进行装配的是引用。
装配集合
当配置集合类型的Bean属性时,Spring提供了4种类型的集合配置元素。:
一、装配Array、List和 Set
当装配类型为数组或者java.util.Collection任意实现的属性时,List和Set这两个配置元素在使用时几乎可以完全互换。
无论<\list>还是<\set>都可以用来装配类型为java.util.Collection的任意实现或者数组的属性。不能因为属性为java.util.Set类型,就表示用户必须使用<\set>元素来完成装配。也可以用<\set>元素来装配java.util.List类型的属性,只是要确保List中的每一个成员都是唯一的。
1、装配字面量集合
public void setAgeList(List<Integer> ageList) {
this.ageList = ageList;
}
public void setCompSet(Set<CompactDisc> compSet) {
this.compSet = compSet;
}
public void setNameArray(String[] nameArray) {
this.nameArray = nameArray;
}
<bean id="duke" class="org.springaction.Juggler">
<!--注入list类型的Integer值-->
<property name="ageList">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<!--注入set类型的bean值-->
<property name="compSet">
<set>
<ref bean="sgtPeppers"/>
<ref bean="cdPlat"/>
</set>
</property>
<!--注入数组的String值-->
<property name="nameArray">
<list>
<value>good</value>
<value>nice</value>
</list>
</property>
</bean>
也可以在构造函数中配置集合类型的值:
public Juggler(List<Integer> ageList,Set<CompactDisc> comSet){
this.ageList=ageList;
this.compSet=comSet;
}
<constructor-arg name="ageList">
<list>
<value>123</value>
<value>456</value>
<value>789</value>
</list>
</constructor-arg>
<constructor-arg name="comSet">
<set>
<ref bean="cdPlat"/>
<ref bean="sgtPeppers"/>
</set>
</constructor-arg>
二、装配Map集合
private Map<String,String> nameMap;
private Map<String,CompactDisc> compactMap;
<property name="nameMap">
<map>
<entry key="zhangsan" value="what"/>
<entry key="lisi" value="up"/>
</map>
</property>
<property name="compactMap">
<map>
<entry key="lei" value-ref="cdPlat"/>
<entry key="li" value-ref="sgtPeppers"/>
</map>
</property>
Map中的entry元素由一个键和一个值组成,键和值可以是简单类型,也可以是其他Bean的引用。Map允许键和值是任意类型:
三、装配Properties集合
限定键和值必须是String类型:
private Properties namePro;
<property name="namePro">
<props>
<prop key="name1">han</prop>
<prop key="name2">mei</prop>
</props>
</property>
四、装配空值
private CompactDisc nullCompact;
//可以覆盖自动装配的值
<property name="nullCompact">
<null/>
</property>
五、混合装配:
可以将JavaConfig的组件扫描和自动装配或/和XML配置混合在一起。在自动装配时,并不在意要装配的bean来自哪里,会考虑容器中的所有的bean,不管它是在JavaConfig或XML中声明的还是通过组件扫描获得的。
1、在JavaConfig中引用其他JavaConfig配置
@Configuration
public class DispactConfig {
public CompactDisc compactDisc(){
return new SgtPeppers();
}
}
@Configuration
//使用Import注解导入,相当于两个配置文件合二为一(可以参见另一篇文章)
@Import(DispactConfig.class)
public class CDPlayConfig {
@Bean
@Autowired
public CompactDisc cdPlay(CompactDisc compactDisc){
CDPlat cdPlat=new CDPlat(compactDisc);
return cdPlat;
}
}
2、在JavaConfig中引用XML配置
@Configuration
//使用ImportResource注解
@ImportResource("classpath:smart.xml")
public class CDPlayConfig {
@Bean
@Autowired
public CompactDisc cdPlay(CompactDisc compactDisc){
CDPlat cdPlat=new CDPlat(compactDisc);
return cdPlat;
}
}
3、XML配置引用其他XML中的配置
<import resource="spring-idol.xml"/>
<bean id="juggler" class="org.springaction.Juggler"
c:cd="sgtPeppers"/>
4、在XML中引用JavaConfig中的配置
<!--如果是通过上下文扫描加载@Configuration配置类-->
<context:component-scan base-package="org.springaction"
resource-pattern="YourJavaConfig.class"/>
<!--否则,看做普通的类,直接配置<bean>-->
<!--不用配置id,不会真正用到config的实例,主要是它里面的实例化bean-->
<bean class="org.config.CDPlayConfig"/>
<bean id="juggler" class="org.springaction.Juggler"
c:cd="compactDisc"/>