spring入门(8)--装配Bean之自动装配 (一)

2014-11-24 11:14:44 · 作者: · 浏览: 0

Spring_Autowiring collaborators

在Spring3.2.2中自动装配类型,分别为:no(default)(不采用自动装配)、byName,byType,constructor下面来分别介绍一下这些是如何自动装配的

Mode Explanation

no: (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类

[java]
package www.csdn.spring.autowire.bean;

public class CumputerBean {

// 电脑名称

private String name;

public void setName(String name) {

this.name = name;

}

}
[java]
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;

}

}
2、创建DeptBean 类

[java]
package www.csdn.spring.autowire.bean;


public class DeptBean {

//部门名称

private String name;

public void setName(String name) {

this.name = name;

}

}
[java]
package www.csdn.spring.autowire.bean;


public class DeptBean {

//部门名称

private String name;

public void setName(String name) {

this.name = name;

}

}

package www.csdn.spring.autowire.bean;


public class DeptBean {

//部门名称

private String name;

public void setName(String name) {

this.name = name;

}

}3、创建EmployeeBean

[java]
package www.csdn.spring.autowire.bean;


public class EmployeeBean {

private DeptBean deptBean;

private CumputerBean cumputerBean;


public void setDeptBean(DeptBean deptBean) {

this.deptBean = deptBean;

}

public void setCumputerBean(CumputerBean cumputerBean) {

this.cumputerBean = cumputerBean;

}

@Override

public String toString() {

return "EmployeeBean [deptBean=" + deptBean + ", cumputerBean="

+ cumputerBean + "]";

}


}
[java]
package www.csdn.spring.autowire.bean;


public class EmployeeBean {

private DeptBean deptBean;

private CumputerBean cumputerBean;


public void setDeptBean(DeptBean deptBean) {

this.deptBean = deptBean;

}

public void setCumputerBean(CumputerBean cumputerBean) {

this.cumputerBean = cumputerBean;

}

@Override

public String toString() {

return "EmployeeBean [deptBean=" + deptBean + ", cumputerBean="

+ cumputerBean + "]";

}


}

package www.csdn.spring.autowire.bean;


public class EmployeeBean {

private DeptBean deptBean;

private CumputerBean cumputerBean;


pu