你的位置:首页 > 软件开发 > Java > Spring中的自动装配案例分析

Spring中的自动装配案例分析

发布时间:2015-12-21 21:00:08
Spring_Autowiring collaborators在Spring3.2.2中自动装配类型,分别为:no(default)(不采用自动装配)、byName,byType,constructor下面来分别介绍一下这些是如何自动装配的 <bean ...

Spring_Autowiring collaborators

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

   <bean id="foo"  autowire="autowire type">

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类

package www.csdn.spring.autowire.bean;public class CumputerBean {// 电脑名称private String name;public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "CumputerBean [name=" + name + "]";}}
 

原标题:Spring中的自动装配案例分析

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录