Spring3.0以后的版本中,Bean自动装配的有以下几种方式:
autowire参数
-
byName 根据id/name
-
byType 根据类名
-
constructor 根据构造参数
-
no 人工指定(默认)
autodetect方法在3.0版本以后已被遗弃
根据id/name自动装配
新建AutowiredBean包,新建NameBean类、WireByNameBean类
新建autowiredBean.xml
新建单元测试AutowiredBeanTest
NameBean类
package AutowiredBean;
import java.text.MessageFormat;
public class NameBean {
public void run() {
System.out.println(MessageFormat.format("这是类:{0}", this.getClass()));
}
}
WireByNameBean类
package AutowiredBean;
public class WireByNameBean {
private NameBean nameBean;
public void setNameBean(NameBean nameBean) {
this.nameBean = nameBean;
}
public NameBean getNameBean() {
return nameBean;
}
public void run() {
nameBean.run();
System.out.println("通过name自动装配类");
}
}
XML配置
<bean id="wireByNameBean" class="AutowiredBean.WireByNameBean" autowire="byName"/>
<bean id="nameBean" class="AutowiredBean.NameBean"/>
单元测试
@Test
public void wireByNameTest() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("autowiredBean.xml");
WireByNameBean wireByNameBean = applicationContext.getBean("wireByNameBean", WireByNameBean.class);
wireByNameBean.run();
}
根据type自动装配
新建TypeBean类、WireByTypeBean类
TypeBean类
package AutowiredBean;
import java.text.MessageFormat;
public class TypeBean {
public void run(){
System.out.println(MessageFormat.format("这是类:{0}",this.getClass()));
}
}
WireByTypeBean类
package AutowiredBean;
public class WireByTypeBean {
private TypeBean typeBean;
public void setTypeBean(TypeBean typeBean) {
this.typeBean = typeBean;
}
public TypeBean getTypeBean() {
return typeBean;
}
public void run(){
typeBean.run();
System.out.println("通过type自动装配类");
}
}
XML配置
<bean id="wireByTypeBean" class="AutowiredBean.WireByTypeBean" autowire="byType"/>
<bean class="AutowiredBean.TypeBean"/>
单元测试
@Test
public void wireByTypeTest() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("autowiredBean.xml");
WireByTypeBean wireByTypeBean = applicationContext.getBean("wireByTypeBean", WireByTypeBean.class);
wireByTypeBean.run();
}
根据构造参数自动装配
新建ConstructBean类、WireByConstructBean类
ConstructBean类
package AutowiredBean;
import java.text.MessageFormat;
public class ConstructBean {
ConstructBean(){
System.out.println(MessageFormat.format("这是类初始化:{0}",this.getClass()));
}
public void run(){
System.out.println(MessageFormat.format("这是类:{0}",this.getClass()));
}
}
WireByConstructBean类
package AutowiredBean;
import java.text.MessageFormat;
public class WireByConstructBean {
WireByConstructBean(ConstructBean constructBean){
System.out.println(MessageFormat.format("这是类初始化:{0}",this.getClass()));
}
public void run(){
System.out.println(MessageFormat.format("这是类:{0}",this.getClass()));
}
}
XML配置
<bean id="wireByConstructBean" class="AutowiredBean.WireByConstructBean" autowire="constructor"/>
<bean id="constructBean" class="AutowiredBean.ConstructBean"/>
单元测试
@Test
public void wireByConstructTest() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("autowiredBean.xml");
WireByConstructBean wireByConstructBean = applicationContext.getBean("wireByConstructBean", WireByConstructBean.class);
wireByConstructBean.run();
}
人工指定
新建ManualWireBean类
ManualWireBean类
package AutowiredBean;
import java.text.MessageFormat;
public class ManualWireBean {
private NameBean nameBean;
public ManualWireBean(ConstructBean constructBean) {
constructBean.run();
}
public NameBean getNameBean() {
return nameBean;
}
public void setNameBean(NameBean nameBean) {
this.nameBean = nameBean;
}
public void run() {
nameBean.run();
System.out.println(MessageFormat.format("这是类:{0}", this.getClass()));
}
}
XML配置
<bean id="manualWireBean" class="AutowiredBean.ManualWireBean" autowire="no">
<property name="nameBean" ref="nameBean"/>
<constructor-arg name="constructBean" ref="constructBean"/>
</bean>
单元测试
@Test
public void manualWireTest() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("autowiredBean.xml");
ManualWireBean manualBean = applicationContext.getBean("manualWireBean", ManualWireBean.class);
manualBean.run();
}
源码下载
git@github.com:Angryshark128/Practice.git