在xml配置文件中
模式 說明
no 不使用自動裝配
byName 根據屬性名自動裝配
屬性完全一致的bean
byType 如果容器中存在一個與指定屬性類型相同的bean
該屬性自動裝配
常
到相匹配的bean
dependency
constructor 與byType方式類似
果容器中沒有找到與構造器參數類型一致的bean
異常
autodetect 通過bean類的自省機制(introspection)來決定是使用
constructor還是byType方式進行自動裝配
構造器
default
看代碼
package com
public class Computer {
private Host host;
private Display display;
public Computer(){
}
public Computer(Host host
super();
this
this
}
public void run(){
System
System
System
}
public Host getHost() {
return host;
}
public void setHost(Host host) {
this
}
public Display getDisplay() {
return display;
}
public void setDisplay(Display display) {
this
}
}
Host
package com
public class Host {
public String run() {
return
}
}
Display
package com
public class Display {
public String run() {
return
}
}
TestMain
package com
import mons
import mons
import org
import orgntext
import orgntext
public class TestMain {
private static Log log = LogFactory
public static void main(String[] args) {
Propnfigure(
log
ApplicationContext ac = new ClassPathXmlApplicationContext(
//byName
Computer computer
System
computer
//byType
Computer computer
System
computer
//default
Computer computer
System
computer
//autodetect
Computer computer
System
computer
//constructor
Computer computer
System
computer
//no
Computer computer
System
computer
log
}
}
applicationContext
<?xml version=
<beans xmlns=
xmlns:xsi=
xsi:schemaLocation=
default
<bean id=
<bean id=
<!
<bean id=
<bean id=
<bean id=
<bean id=
<!
<bean id=
<constructor
<ref bean=
</constructor
<constructor
<ref bean=
</constructor
</bean>
<bean id=
<constructor
<ref bean=
</constructor
<constructor
<ref bean=
</constructor
</bean>
</beans>
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28670.html