Spring入门(5)-自动装配Bean属性
Spring入门(5)-自动装配Bean属性
本文介绍如何装配Bean属性。
0. 目录
- ByName
- ByType
- constructor
- 默认自动装配
- 混合使用自动装配和显示装配
1. ByName
把与Bean的属性具有相同名字(或ID)的其他Bean自动装配到Bean对应的属性中。如果没有跟属性名称相匹配的Bean,则该属性不进行装配。
package com.chzhao.springtest;
public class PersonBll implements IPersonBll {
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public void show() {
System.out.println(this.person.getName());
}
}
<?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">
<bean name="person" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll" autowire="byName">
</bean>
</beans>
2. ByType
把与Bean的属性具有相同类型的其他Bean自动装配到Bean对应的属性中。如果没有跟属性类型相匹配的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll" autowire="byType">
</bean>
</beans>
如上,如果设置为byType,Bean的名字随便设置了,不需要为person
3. constructor
把与Bean的构造器入参具有相同类型的其他Bean自动装配到Bean构造器的对应入参中。
package com.chzhao.springtest;
public class PersonBll implements IPersonBll {
public PersonBll(Person person) {
this.person = person;
}
private Person person;
public void show() {
System.out.println(this.person.getName());
}
}
<?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">
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value ="老王"></property>
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll" autowire="constructor">
</bean>
</beans>
4. 默认自动装配
Spring可以通过设置default-autowire使所有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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="constructor">
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value ="老王"></property>
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll">
</bean>
</beans>
5. 混合使用自动装配和显示装配
即使对某个Bean的属性采用了自动装配,仍然可以通过显示装配配置某个属性。
package com.chzhao.springtest;
public class PersonBll implements IPersonBll {
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
private Person person;
public void show() {
System.out.println(this.person.getName());
}
}
<?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"
default-autowire="byType"
>
<bean name="laowang" class="com.chzhao.springtest.Person">
<property name="name" value="老王" />
</bean>
<bean name="laoli" class="com.chzhao.springtest.Person">
<property name="name" value="老李" />
</bean>
<bean name="PersonBll" class="com.chzhao.springtest.PersonBll" >
<property name="person" ref="laoli"></property>
</bean>
</beans>