在xml配置文件中autowire有种类型可以在<bean/>元素中使用autowire属性指定
模式说明
no不使用自动装配必须通过ref元素指定依赖默认设置
byName根据属性名自动装配此选项将检查容器并根据名字查找与
属性完全一致的bean并将其与属性自动装配
byType如果容器中存在一个与指定属性类型相同的bean那么将与
该属性自动装配如果存在多个该类型bean那么抛出异
常并指出不能使用byType方式进行自动装配如果没有找
到相匹配的bean则什么事都不发生也可以通过设置
dependencycheck=objects让Spring抛出异常
constructor与byType方式类似不同之处在于它应用于构造器参数如
果容器中没有找到与构造器参数类型一致的bean那么抛出
异常
autodetect通过bean类的自省机制(introspection)来决定是使用
constructor还是byType方式进行自动装配如果发现默认的
构造器那么将使用byType方式
default
看代码Computerjava
packagecomredoffice;
publicclassComputer{
privateHosthost;
privateDisplaydisplay;
publicComputer(){
}
publicComputer(HosthostDisplaydisplay){
super();
thishost=host;
thisdisplay=display;
}
publicvoidrun(){
Systemoutprintln();
Systemoutprint(+hostrun()+);
Systemoutprintln(displayrun());
}
publicHostgetHost(){
returnhost;
}
publicvoidsetHost(Hosthost){
thishost=host;
}
publicDisplaygetDisplay(){
returndisplay;
}
publicvoidsetDisplay(Displaydisplay){
thisdisplay=display;
}
}
Hostjava
packagecomredoffice;
publicclassHost{
publicStringrun(){
return我是主机正在运行!;
}
}
Displayjava
packagecomredoffice;
publicclassDisplay{
publicStringrun(){
return我是显示器正在运行!;
}
}
TestMainjava
packagecomredoffice;
importmonsloggingLog;
importmonsloggingLogFactory;
importorgapachelogjPropertyConfigurator;
importorgntextApplicationContext;
importorgntextsupportClassPathXmlApplicationContext;
publicclassTestMain{
privatestaticLoglog=LogFactorygetLog(TestMainclass);
publicstaticvoidmain(String[]args){
Propnfigure(bin/logjproperties);
logdebug(enteringmainmethod);
ApplicationContextac=newClassPathXmlApplicationContext(
applicationContextxml);
//byName
Computercomputer=(Computer)acgetBean(computer);
Systemoutprintln(autowire=\byName\:);
computerrun();
//byType
Computercomputer=(Computer)acgetBean(computer);
Systemoutprintln(autowire=\byType\:);
computerrun();
//default
Computercomputer=(Computer)acgetBean(computer);
Systemoutprintln(autowire=\default\:);
computerrun();
//autodetect
Computercomputer=(Computer)acgetBean(computer);
Systemoutprintln(autowire=\autodetect\:);
computerrun();
//constructor
Computercomputer=(Computer)acgetBean(computer);
Systemoutprintln(autowire=\constructor\:);
computerrun();
//no
Computercomputer=(Computer)acgetBean(computer);
Systemoutprintln(autowire=\no\:);
computerrun();
logdebug(leavingmainmethod);
}
}
applicationContextxml
<?xmlversion=encoding=UTF?>
<beansxmlns=
xmlns:xsi=instance
xsi:schemaLocation=;beansxsd
defaultautowire=autodetect>
<beanid=hostname=hostclass=comredofficeHost></bean>
<beanid=displayname=displayclass=comredofficeDisplay></bean>
<!autowire有noconstructorbyNamebyTypedefaultautodetect几种状况>
<beanid=computerclass=comredofficeComputerautowire=byName></bean>
<beanid=computerclass=comredofficeComputerautowire=byType></bean>
<beanid=computerclass=comredofficeComputerautowire=default></bean>
<beanid=computerclass=comredofficeComputerautowire=autodetect></bean>
<!在Computer类中必须有一个无参和有参的构造函数否则报错>
<beanid=computerclass=comredofficeComputerautowire=constructor>
<constructorargindex=>
<refbean=host/>
</constructorarg>
<constructorargindex=>
<refbean=display/>
</constructorarg>
</bean>
<beanid=computerclass=comredofficeComputerautowire=no>
<constructorargindex=>
<refbean=host/>
</constructorarg>
<constructorargindex=>
<refbean=display/>
</constructorarg>
</bean>
</beans>