Auto-wiring in Spring

Posted: December 9, 2011 in Spring

In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just define the “autowire” attribute in <bean>.

There are 5 auto-wiring modes in spring

  1. no – Default, no auto wiring, set it manually via “ref” attribute
  2. byName – Auto wiring by property name. If the name of a bean is same as the name of other bean property, auto wire it.
  3. 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.
  4. constructor – byType mode in constructor argument.
  5. autodetect – If a default constructor is found, use “autowired by constructor”; Otherwise, use “autowire by type”.

Hope the following examples will help you understand auto-wiring much better

Consider the following classes – Customer.java and Person.java

public class Customer {
        private Person person;

        public Customer(Person person) {
            this.person = person;
        }

        public void setPerson(Person person) {
            this.person = person;
        }
    }

    public class Person {
        // some code..
    }

1. NO

This is the default mode. We have to use <ref> tag here

<bean id="customer" class="de.suhas.Customer">
          <property name="person" ref="person" />
</bean>

<bean id="person" class="de.suhas.Person" />

2. 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="de.suhas.Customer" autowire="byName" />
<bean id="person" class="de.suhas.Person" />

3. 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. so, Spring will auto wired it via setter method – “setPerson(Person person)“.

<bean id=”customer” class=”de.suhas.Customer” autowire=”byType” />
<bean id=”person” class=”de.suhas.Person” />

4. constructor

Auto-wire a bean by property data type in constructor argument.
This is used when we go for constructor based dependency injection.
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=”de.suhas.Customer” autowire=”constructor” />
<bean id=”person” class=”de.suhas.Person” />

5. autodetect

If a default constructor is found, uses “constructor”, otherwise, uses “byType”.
<bean id="customer" class="de.suhas.common.Customer" autowire="autodetect" />
<bean id="person" class="de.suhas.Person" />
Tip : 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="de.suhas.Customer"
			autowire="autodetect" dependency-check="objects />
<bean id="person" class="de.suhas.Person" />
Advertisements
Comments
  1. sai says:

    Good explanation…

    Can you just explain me which one is the better way and why..?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s