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" />

See full example – Spring Autowiring by Name.

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" />

See full example – Spring Autowiring by Type.

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" />

See full example – Spring Autowiring by Constructor.

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" />

See full example – Spring Autowiring by AutoDetect.

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.

About the Author

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

Comments

Leave a Reply

avatar
newest oldest most voted
sathyajith
Guest
sathyajith

Thanks a lot Mkyong, Your way of writing is very simple to grab. usually ability to explain this much clear shows how clearly and comprehensively You know the subject …End to End. ..You are doing noble work….

Ranjith
Guest
Ranjith

great and simple explanation for spring newbies.earlier i had referred some books to understand this auto wiring concept,but your explanation solved my problem.

Engin
Guest
Engin

“no – Default, no auto wiring, set it manually via “ref” attribute” –> this is FALSE !
The default is ‘by Type’ !!!

rr
Guest
rr

This confused me initially. Please put in the header that this applies only to XML based a/w.

hariii
Guest
hariii

what is meant by dependency check here???

PosTi85
Guest
PosTi85

If there isn’t any matching to autowire in a bean which has an object property that should be set, it would throw an exception

rocky
Guest
rocky

thanks

Bashi Bazouk
Guest
Bashi Bazouk

What do you mean by ‘wire’? I am trying to find what is meant by auto-wiring and diving in straight in does not help.

Prem Tiwari
Guest
Prem Tiwari

I think you dont need to go anywhere such a crystal clear explanation.

Thanks alot for your efforts Sir ..please keep posting. God bless you. :)

sites
Guest
sites

I in addition to my friends happened to be reading through the best tips from your site and instantly I had a terrible feeling I never expressed respect to the web site owner for them. All of the men were definitely as a consequence excited to read through all of them and have clearly been loving those things. Appreciate your really being simply helpful and then for deciding upon these kinds of superb things millions of individuals are really eager to be informed on. Our own sincere regret for not saying thanks to you sooner.

trackback
Spring autowire | Giovanni's Blog
benson
Guest
benson

I think there is a typo in your note.
Missing a mark ‘”‘ in the attribute dependency-check.
BTW,I’m your big fan.

rm
Guest
rm

“there is a default constructor in “Customer” class”
There isn’t.

I thought, default constructor means:

public Customer() {
  // something
}

Without any arguments.

Why do you call it default?

Arun
Guest
Arun

simple and sweet!

Raju
Guest
Raju

Easy to learn basic concepts.

suresh
Guest
suresh

very nice..

Nagesh
Guest
Nagesh

It is very nice tutorials;
Excellent effort for bring all most all JAVA/JAVA frame works on single thread.
It is very useful for me to revise quickly.
Thanks a lot for your efforts.
Please keep posting such type of atricles.

Vivek
Guest
Vivek

This is not what I was looking for. You stink!!

Roshan
Guest
Roshan

Please do not discourage.
He is doing a very good work.
I have been greatly entertained by his articles as part of my interview preparations.

Prashant Pratap Singh
Guest
Prashant Pratap Singh

Very simple to understand.
Thanks a lot.

Dhandapani S
Guest
Dhandapani S

Wonder full explanation…. thanks

nagraj palwencha
Guest
nagraj palwencha

good explanation with example
Thanks a lot

Siddhraj Atodaria
Guest
Siddhraj Atodaria

Great…..
Thanks ……….

trackback
Spring and Hibernate » Agile Mobile Developer
Natraj
Guest
Natraj
 Very nice tutorial. 
Manish
Guest
Manish

Wonderfully simple & good tutorial,
Thanks

trackback
Spring Tutorials | Tutorials

[…] Auto-Wiring Beans in XML Examples to show how to define ‘auto-wiring’ modes in bean configuration file. […]

trackback
Spring – Auto-Wiring Beans with @Autowired annotation | Spring

[…] Beans with @Autowired annotation Written on March 12, 2010 at 10:06 am by mkyong The auto-wiring in the bean configuration file will wire all properties of a bean. In most cases, you may need wire a particular property only. In […]