zoukankan      html  css  js  c++  java
  • Spring Auto-Wiring Beans

    In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just define the “autowire” attribute in <bean>.

    	<bean id="customer" class="com.mkyong.common.Customer" autowire="byName" />
    

    In Spring, 5 Auto-wiring modes are supported.

    • no – Default, no auto wiring, set it manually via “ref” attribute
    • byName – Auto wiring by property name. If the name of a bean is same as the name of other bean property, auto wire it.
    • byType – Auto wiring by property data type. If data type of a bean is compatible with the data type of other bean property, auto wire it.
    • constructor – byType mode in constructor argument.
    • autodetect – If a default constructor is found, use “autowired by constructor”; Otherwise, use “autowire by type”.

    Examples

    A Customer and Person object for auto wiring demonstration.

    package com.mkyong.common;
    
    public class Customer 
    {
    	private Person person;
    	
    	public Customer(Person person) {
    		this.person = person;
    	}
    	
    	public void setPerson(Person person) {
    		this.person = person;
    	}
    	//...
    }
    
    package com.mkyong.common;
    
    public class Person 
    {
    	//...
    }
    

    1. Auto-Wiring ‘no’

    This is the default mode, you need to wire your bean via ‘ref’ attribute.

    	<bean id="customer" class="com.mkyong.common.Customer">
                      <property name="person" ref="person" />
    	</bean>
    
    	<bean id="person" class="com.mkyong.common.Person" />
    

    2. Auto-Wiring ‘byName

    Auto-wire a bean by property name. In this case, since the name of “person” bean is same with the name of the “customer” bean’s property (“person”), so, Spring will auto wired it via setter method – “setPerson(Person person)“.

    	<bean id="customer" class="com.mkyong.common.Customer" autowire="byName" />
    	
    	<bean id="person" class="com.mkyong.common.Person" />
    

    3. Auto-Wiring ‘byType

    Auto-wire a bean by property data type. In this case, since the data type of “person” bean is same as the data type of the “customer” bean’s property (Person object), so, Spring will auto wired it via setter method – “setPerson(Person person)“.

    	<bean id="customer" class="com.mkyong.common.Customer" autowire="byType" />
    	
    	<bean id="person" class="com.mkyong.common.Person" />
    

    4. Auto-Wiring ‘constructor

    Auto-wire a bean by property data type in constructor argument. In this case, since the data type of “person” bean is same as the constructor argument data type in “customer” bean’s property (Person object), so, Spring auto wired it via constructor method – “public Customer(Person person)“.

    	<bean id="customer" class="com.mkyong.common.Customer" autowire="constructor" />
    	
    	<bean id="person" class="com.mkyong.common.Person" />
    

    5. Auto-Wiring ‘autodetect’

    If a default constructor is found, uses “constructor”; Otherwise, uses “byType”. In this case, since there is a default constructor in “Customer” class, so, Spring auto wired it via constructor method – “public Customer(Person person)“.

    	<bean id="customer" class="com.mkyong.common.Customer" autowire="autodetect" />
    	
    	<bean id="person" class="com.mkyong.common.Person" />
    

    Note
    . It’s always good to combine both ‘auto-wire’ and ‘dependency-check’ together, to make sure the property is always auto-wire successfully.

    	<bean id="customer" class="com.mkyong.common.Customer" 
    			autowire="autodetect" dependency-check="objects />
    	
    	<bean id="person" class="com.mkyong.common.Person" />
    

    Conclusion

    In my view, Spring ‘auto-wiring’ make development faster with great costs – it added complexity for the entire bean configuration file, and you don’t even know which bean will auto wired in which bean.

    In practice, i rather wire it manually, it is always clean and work perfectly, or better uses @Autowired annotation, which is more flexible and recommended.

  • 相关阅读:
    winform 单选框, 图像控件,图像列表,状态栏,定时器,绘图
    学习资料链接,csdn博客
    c# 基本控件,窗口程序
    Gmap.net 怎么导入离线地图
    修改MFC主窗口界面标题和图标的方法
    vc6.0缓冲区
    vc6.0编译出错,删除多余的文件,清空重新编译
    Tomcat插件与Jetty插件在MyEclipse中的配置
    div里面的内容超出自身高度时,显示省略号
    aptana studio 3 自动换行(无需插件)
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4749904.html
Copyright © 2011-2022 走看看