Autowiring in Spring

We have already learnt how to declare Spring beans and inject bean using <property> (Setter Injection) and <constructor-arg> (Constructor Injection) in the XML configuration file.

Autowiring is a feature of Spring framework which lets you to Inject Dependency implicitly. Basically it will be using internally using Setter Injection / Constructor Injection.

Point to be noted here is Autowiring can’t be used to inject primitive and string values. It works only with objects.

Types of Autowiring

Spring Supports the following Autowiring

  1. none – It is the default one, it means no autowiring by default.
  2. byNameThis injects dependency based on bean name, in this case bean id and the property name must be the same.
  3. byType – This injects dependecy based type(class), in this case bean id and the property name can be different.
  4. constructorThis is similar to byType autowiring, but type applies to constructor arguments.
  5. autodetect – Autowiring by autodetect uses either of two modes i.e. constructor or byType modes. First it tries to autowire by constructor if it doesn’t work then it tires to autowire byType.

Examples of Autowiring

Autowiring byName example:

In this type of autowiring, the property name and bean id has to be the same.

public class Bean1 
{
    public Bean2 b2;
    public Bean2 getB2() {
        return b2;
    }
    public void setB2(Bean2 b2) {
        this.b2 = b2;
    }
}
public class Bean2 
{
}

Configuration will be

 <bean id="b1" class="com.javainterviewpoint.Bean1" autowire="byName"></bean>
 <bean id="b2" class="com.javainterviewpoint.Bean2"></bean>

Autowiring byType example:

In this type of autowiring, the property name and bean id need not be same. Property’s class type is used for searching a matching bean definition in configuration file.

public class Bean1 
{
    public Bean2 b2;
    public Bean2 getB2() {
        return b2;
    }
    public void setB2(Bean2 b2) {
        this.b2 = b2;
    }
}
public class Bean2 
{
}

Configuration will be

 <bean id="b1" class="com.javainterviewpoint.Bean1" autowire="byType"></bean>
 <bean id="b2" class="com.javainterviewpoint.Bean2"></bean>

Autowiring constructor example:

This type of autowiring is similar to byType autowiring, but applies to constructor arguments. In autowire enabled bean, it will look for class type of constructor arguments, and then do a autowire by type on all constructor arguments.

public class Bean1 
{
    public Bean2 b2

	public Bean1(Bean2 b2) 
	{
		this.b2 = b2;
	}
}
public class Bean2 
{
}

Configuration will be

 <bean id="b1" class="com.javainterviewpoint.Bean1" autowire="constructor"></bean>
 <bean id="b2" class="com.javainterviewpoint.Bean2"></bean>

Autowiring autodetect:

Autodetect will first tries to autowire by constructor if it doesn’t work then it tires to autowire byType.

Point to be Noted :  autodetect is deprecated from Spring 3

No Autowiring :

Autowiring will be enabled unless and until you explicitly mention it in your configuration file. If nothing is mentioned then it is the default one(no autowiring)

Configuration will be

 <bean id="b1" class="com.javainterviewpoint.Bean1"></bean>
 <bean id="b2" class="com.javainterviewpoint.Bean2"></bean>

Leave a Reply

Your email address will not be published. Required fields are marked *