Spring Autowiring

Spring Autowiring

Autowiring is an essential feature of the Spring framework. The autowiring function is used for wiring beans automatically, without injecting the beans using <constructor-arg> and <property> elements in the bean-config file. It can also autowire a relationship without combining beans.

As compared to the <property> and <constructor-arg> tag, autowiring needs less specification. Autowiring acts as a very powerful feature in developing applications, as it requires less configuration code and developing time.

With the Spring 2.5, an alternative to XML- based configuration is provided by the annotation-based configuration. Instead of using the XML bean-configuration file for bean wiring, the autowiring is done through the @Autowired annotation. It provides the same functionalities as the legacy autowiring methods. In the conventional way of autowiring or XML- based configuration, different modes of autowiring are available in the Spring framework.

Following are the different modes of autowiring used conventionally:

  • no – It means No autowiring by default. The beans are referenced by the ref element in the bean-config file. It merely refers to the dependency injection.
  • autowire byName – In this mode, the object dependency is injected according to the name of the bean. The bean name must be the same as the property name; else, it throws an exception.
  • autowire byType –  In this mode, the class type is used for autowiring. In the bean-configuration file, only one bean should be available for autowiring byType. A fatal exception is thrown if more than one bean exists in the configuration file.
  • autowire by constructor –In this mode, a  constructor is used for autowiring. It is very similar to the autowiring byType, but the only difference is that the constructor is used instead of type.

Developers use @Autowired annotation instead of XML-based configuration for autowiring beans after the Spring Version 2.5. Annotation-based autowiring is more flexible than an XML-based configuration.  

@Autowired annotation

In Spring, the @Autowired annotation is used to autowire beans automatically. The @Autowired annotation can be used with the variables and setter methods for autowiring byType. It can also be used with the constructor for constructor based autowiring in Spring. The @Autowired annotation is available in the org.springframework.beans.factory.annotation package.                                  

Autowiring Modes using @Autowired

The Spring framework contains three modes of autowiring, which are listed below:

  • Constructor Injection In this mode, the constructor of beans are wired using the @Autowired annotation.

Following code snippet shows how to use Constructor Injection:

The @Autowired annotation is defined just above the constructor of the bean.

@Component
public class Cricket_Coach implements Coach {
private Fortune fortuneservice;
// defining the Constructor
@Autowired
public Cricket_Coach(Fortune fortuneservice) {
super(); 
this.fortuneservice = fortuneservice;
}
} 
  • Setter Injection In this mode, the setter method of beans are wired using the @Autowired annotation.

Following code snippet shows how to use Setter Injection:

The @Autowired annotation is defined just before the setter method of the bean.

@Component
public class Cricket_Coach implements Coach {
private Fortune fortuneservice;
// defining the setter method
public void setFortuneservice(Fortune fortuneservice) {
this.fortuneservice = fortuneservice;
}
} 
  • Field Injection In this mode, the fields or parameters are wired using the @Autowired annotation.

Following code snippet shows how to use Field Injection:

The @Autowired annotation is defined just before the field of the bean class.

@Component
public class Cricket_Coach implements Coach {
// defining the Field
@Autowired
private Fortune fortuneservice;
} 

Advantages of Autowiring

Following are the advantages of autowiring:

  • Requires less configuration code – It requires less configuration code as autowiring itself is responsible for injecting the beans implicitly. We don’t have to write the injection code explicitly in the bean-config file.
  • Reduces the developing time – It reduces the developing time as it removes the need for specifying the property and constructor arguments in the bean-config file.
  • Flexible – It provides several modes for autowiring, and gives the flexibility to choose any autowiring type.

Disadvantages of Autowiring

Following are the disadvantages of autowiring:

  • Confusing nature – As compared to the explicit injection of beans, autowiring is not that exact. For example, if there are many constructors available in the Spring container for constructor autowiring, Spring won’t be able to determine which of those beans has to be wired.
  • Primitive data types – We cannot define primitive data types such as String and integer using autowiring. They can be set through explicit injection.