zoukankan      html  css  js  c++  java
  • Spring Autowiring by Name

    In Spring, “Autowiring by Name” means, if the name of a bean is same as the name of other bean property, auto wire it.

    For example, if a “customer” bean exposes an “address” property, Spring will find the “address” bean in current container and wire it automatically. And if no matching found, just do nothing.

    You can enable this feature via autowire="byName" like below :

    	<!-- customer has a property name "address" -->
    	<bean id="customer" class="com.mkyong.common.Customer" autowire="byName" />
    	
    	<bean id="address" class="com.mkyong.common.Address" >
    		<property name="fulladdress" value="Block A 888, CA" />
    	</bean>
    

    See a full example of Spring auto wiring by name.

    1. Beans

    Two beans, customer and address.

    package com.mkyong.common;
     
    public class Customer 
    {
    	private Address address;
    	//...
    }
    
    package com.mkyong.common;
     
    public class Address 
    {
    	private String fulladdress;
    	//...
    }
    

    2. Spring Wiring

    Normally, you wire the bean explicitly, via ref attribute like this :

    	<bean id="customer" class="com.mkyong.common.Customer" >
    		<property name="address" ref="address" />
    	</bean>
    	
    	<bean id="address" class="com.mkyong.common.Address" >
    		<property name="fulladdress" value="Block A 888, CA" />
    	</bean>
    

    Output

    Customer [address=Address [fulladdress=Block A 888, CA]]
    

    With autowire by name enabled, you do not need to declares the property tag anymore. As long as the “address” bean is same name as the property of “customer” bean, which is “address”, Spring will wire it automatically.

    	<bean id="customer" class="com.mkyong.common.Customer" autowire="byName" />
    	
    	<bean id="address" class="com.mkyong.common.Address" >
    		<property name="fulladdress" value="Block A 888, CA" />
    	</bean>
    

    Output

    Customer [address=Address [fulladdress=Block A 888, CA]]
    

    See another example, this time, the wiring will failed, caused the bean “addressABC” is not match the property name of bean “customer”.

    	<bean id="customer" class="com.mkyong.common.Customer" autowire="byName" />
    	
    	<bean id="addressABC" class="com.mkyong.common.Address" >
    		<property name="fulladdress" value="Block A 888, CA" />
    	</bean>
    

    Output

    Customer [address=null]
    
  • 相关阅读:
    SQL Server 2005 中的同义词
    SQL SERVER 2005中同义词实例
    聚集索引和非聚集索引(整理)
    linux kernel中timer的使用
    linux命令: patch
    win7(64位)php5.5-Apache2.4-mysql5.6环境安装
    tasklet和工作队列
    linux串口编程(c)
    Notepad++中Windows,Unix,Mac三种格式
    centos7/redhat7 将网卡名字改成eth样式的方法
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4749926.html
Copyright © 2011-2022 走看看