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

    In Spring, “Autowiring by Constructor” is actually autowiring by Type in constructor argument. It means, if data type of a bean is same as the data type of other bean constructor argument, auto wire it.

    See a full example of Spring auto wiring by constructor.

    1. Beans

    Two beans, developer and language.

    package com.mkyong.common;
    
    public class Developer {
    	private Language language;
    
    	//autowire by constructor
    	public Developer(Language language) {
    		this.language = language;
    	}
    
    	//...
    
    }
    
    package com.mkyong.common;
    
    public class Language {
    	private String name;
    	//...
    }
    

    2. Spring Wiring

    Normally, you wire the bean via constructor like this :

    	<bean id="developer" class="com.mkyong.common.Developer">
    		<constructor-arg>
    			<ref bean="language" />
    		</constructor-arg>
    	</bean>
    		
    	<bean id="language" class="com.mkyong.common.Language" >
    		<property name="name" value="Java" />
    	</bean>
    

    Output

    Developer [language=Language [name=Java]]
    

    With autowire by constructor enabled, you can leave the constructor property unset. Spring will find the compatible data type and wire it automatcailly.

    	<bean id="developer" class="com.mkyong.common.Developer" autowire="constructor" />
    		
    	<bean id="language" class="com.mkyong.common.Language" >
    		<property name="name" value="Java" />
    	</bean>
    

    Output

    Developer [language=Language [name=Java]]
    
  • 相关阅读:
    POJMatrix(二维树状数组)
    HD1556Color the ball(树状数组)
    闲的没事,自挂东南枝
    高端、洋气效果
    “绝对”妹纸~position
    float元素一定要闭合
    dw cs6激活码一枚
    shell 预定义变量
    ubuntu 安装docker
    Microsonf visual c++ 14+ 离线内网安装
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4749929.html
Copyright © 2011-2022 走看看