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]]

Download Source Code

References

  1. Spring Autowiring by Type
  2. Spring Autowiring by Constructor

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
5 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
Pankaj Kumarmahesha999JiteshVijay KumarRick Recent comment authors
newest oldest most voted
Pankaj Kumar
Guest
Pankaj Kumar

isn’t default constructor parameter less , any reason why you are calling parametrized constructor default constructor.

mahesha999
Guest
mahesha999

“default constructor (argument with any data type)”

Wikipedia defines default constructor as follows:

“In computer programming languages the term default constructor can refer to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors”

You definitely dont mean default constructor there, right?

Jitesh
Guest
Jitesh

how is that default constructor? That constructor is taking argument.

Vijay Kumar
Guest
Vijay Kumar

It is no longer working in Spring 3.0.

Rick
Guest
Rick

Hi Mkyong,

Your tutorials are excellent and loved to read them, but I am confused when you use word ‘default constructor’ for argumented constructor. In my learning, I read zero argument construtor is default constructor.
Could you please clarify the doubt.

Thanks,
Rick