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.


Related Topics

Spring - Bean Scopes with Annotations

Spring - Bean Scopes with Annotations We have previously discussed the Spring bean scopes using an XML-based configuration. In the XML-based configuration, we have defined the beans and its scope inside the bean-config...

3 minutes read.

Spring MVC Form Tag Library

The Spring MVC provides a variety of tags available in the form of a tag library, which is used to develop web pages (JSP view pages). The Spring’s form tag...

6 minutes read.

Spring MVC Regular Expression Validation

Example of Regular Expression Validation Here, we are going to create an example of regular expression validation. Following are the steps to create an example of a regular expression: Create a request page In this step, we are going...

13 minutes read.

Spring MVC Form Validation

Spring Validation is used to validate the @Controller inputs. It is used to check and accept the user input in any web application. Using Spring validation, we can validate the user input on...

12 minutes read.

Spring Tutorial

What is Spring Framework? Spring is one of the most popular enterprise application framework used for developing Java applications. “Spring is a lightweight open-source framework. It allows Java developer to build simple, reliable,...

6 minutes read.

Spring - Bean lifecycle using annotations

Spring - Bean lifecycle using annotations We have already discussed the bean lifecycle using an XML-based configuration. In the previous case, we have declared the init-method and destroy-method attributes in the bean-configuration file. Here, we...

2 minutes read.

Spring AOP – @Before Advice Type

Spring AOP – @Before Advice Type As we have discussed, AOP (Aspect-Oriented Programming) is one of the powerful features of the Spring framework. It complements the Object-Oriented Programming (OOPs). AOP provides some concepts and terminology,...

2 minutes read.

Spring AOP – @AfterReturning Advice

Spring AOP – @AfterReturning Advice We have discussed earlier the types of advices available in Spring AOP. One of them is After Returning advice. It is an advice that executes after the normal execution...

3 minutes read.

Spring MVC Tutorial

Spring MVC is a web Model-View-Controller framework built on Servlet API. It is used to build web applications. The MVC framework works around central servlet called DispatcherServlet. It dispatches the requests to the...

4 minutes read.

Spring – Bean Lifecycle

Spring – Bean Lifecycle Spring bean is responsible for managing the lifecycle of beans created by the Spring container.  The lifecycle of the bean is easy to understand. When a bean is created, it requires...

4 minutes read.

Spring – Beans using @Bean

Spring – Beans using @Bean We have previously discussed an example of Spring configuration using an XML-based configuration. In the previous example, we have defined beans inside the bean-configuration file (applicationContext.xml). To make the...

3 minutes read.

Spring MVC Form – Radio Button

A variety of form tags are available in Spring MVC for developing web applications, <form:radiobutton> tag is one of them. The radio button tag is used to choose only one option among a...

10 minutes read.

Spring MVC Form Checkbox

Spring MVC provides several form tags used to develop web applications (Usually JSP pages). One of them is a checkbox tag. The checkbox tag offers us to choose multiple options at the same...

12 minutes read.

Spring Model Interface

Model Interface Model is an interface that defines a holder for model attributes. It is also used to transfer data between various parts of the Spring MVC application. The Model interface is available...

8 minutes read.

Spring Autowiring – Constructor Injection

Spring Autowiring – Constructor Injection We have previously discussed an example of Constructor Dependency Injection (CDI) using an XML-based configuration. In the previous case, we have defined the reference of the constructor in...

2 minutes read.

Spring MVC Form Text Field

We have previously discussed the Spring MVC form tags. The form tag library provides a variety of form tags that are used to build web applications. One of them is a <form:input>...

8 minutes read.

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

3 minutes read.

Spring AOP – @Around Advice

Spring AOP – @Around Advice We have discussed in our previous tutorial, the concepts, and terminology of the Spring AOP. Around advice is one of the types of advices. It can be applied to...

4 minutes read.

Spring MVC Form – Drop-Down List

We have already discussed the Spring MVC form tags available in the form tag library. Form tags are used to develop web applications easily. In this tutorial, we are going to discuss the...

13 minutes read.

Spring - Inversion of Control (IoC)

Spring - Inversion of Control (IoC) The Inversion of Control (IoC) is a process where the objects define their dependencies, that is, the dependencies of other objects with they are working. It is done...

4 minutes read.