spring入门(8) 装配Bean之自动装配2013-07-18 csdn 史国旭Spring_Autowiring collaborators在Spring3.2.2中自动装配类型,分别为:no(default)(不采用自动装配)、byName,byType,constructor下面来分别介绍一下这些是如何自动装配的<bean id="foo" class="...Foo" autowire="autowire type">Mode Explanationno: (Default) No autowiring. Bean references must be defined via a ref element.Changing the default setting is not recommended for larger deployments,because specifying collaborators explicitly gives greater control and clarity.To some extent, it documents the structure of a system.byName:Autowiring by property name.Spring looks for a bean with the same name as the property that needs to be autowired.For example, if a bean definition is set to autowire by name,and it contains a master property (that is, it has a setMaster(..) method),Spring looks for a bean definition named master, and uses it to set the property.byType:Allows a property to be autowired if exactly one bean of the property type exists in the container.If more than one exists, a fatal exception is thrown,which indicates that you may not use byType autowiring for that bean.If there are no matching beans, nothing happens; the property is not set.constructor:Analogous to byType, but applies to constructor arguments.If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised案例分析: 1、创建CumputerBean类
package www.csdn.spring.autowire.bean;public class CumputerBean {// 电脑名称 private String name;public void setName(String name) {this.name = name;}}package www.csdn.spring.autowire.bean;public class CumputerBean {// 电脑名称private String name;public void setName(String name) {this.name = name;}}