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

    In Spring, “Autowiring by AutoDetect“, means chooses “autowire by constructor” if default constructor (argument with any data type), otherwise uses “autowire by type“.
    See an example of Spring “auto wiring by autodetect”. Auto wiring the “kungfu” bean into “panda”, via constructor or type (base on the implementation of panda bean).

    	<bean id="panda" class="com.mkyong.common.Panda" autowire="autodetect" />
    		
    	<bean id="kungfu" class="com.mkyong.common.KungFu" >
    		<property name="name" value="Shao lin" />
    	</bean>
    

    1. AutoDetect – by Constructor

    If a default constructor is supplied, auto detect will chooses wire by constructor.

    package com.mkyong.common;
    
    public class Panda {
    	private KungFu kungfu;
    
    	public Panda(KungFu kungfu) {
    		System.out.println("autowiring by constructor");
    		this.kungfu = kungfu;
    	}
    
    	public KungFu getKungfu() {
    		return kungfu;
    	}
    
    	public void setKungfu(KungFu kungfu) {
    		System.out.println("autowiring by type");
    		this.kungfu = kungfu;
    	}
    
    	//...
    }
    

    Output

    autowiring by constructor
    Person [kungfu=Language [name=Shao lin]]
    

    2. AutoDetect – by Type

    If a default constructor is not found, auto detect will chooses wire by type.

    package com.mkyong.common;
    
    public class Panda {
    	private KungFu kungfu;
    
    	public KungFu getKungfu() {
    		return kungfu;
    	}
    
    	public void setKungfu(KungFu kungfu) {
    		System.out.println("autowiring by type");
    		this.kungfu = kungfu;
    	}
    
    	//...
    }
    

    Output

    autowiring by type
    Person [kungfu=Language [name=Shao lin]]
    
  • 相关阅读:
    Docker学习笔记之常用的 Docker Compose 配置项
    Docker学习笔记之使用 Docker Compose 管理容器
    qt无法使用终端启动的解决方法
    实践卡尔曼滤波--小球追踪
    高斯分布 笔记
    蒙特卡罗定位(Particle Filter Localization)笔记
    珊格地图笔记
    ubuntu14.04 下安装 gsl 科学计算库
    SLAM学习资料汇总
    矩阵的SVD分解
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4750052.html
Copyright © 2011-2022 走看看