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 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 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 Number Range Validation

Example of Number Range Validation Here, we are going to create an example of number range validation. Following are the steps to create an example of number range validation. Create a request page In this step, we are...

12 minutes read.

Spring MVC – @RequestParam

We have already discussed an example of Spring MVC in our previous tutorials. Here, we are going to discuss the @RequestParam annotation. In the last case, we had created a bean class for...

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 – 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 - Bean Scope

Spring - Bean Scope Bean Scope refers to a lifecycle of the bean. It allows us to have control over the creation of bean objects. It is a very powerful approach and provides...

6 minutes read.

Spring AOP – @After Advice

Spring AOP – @After Advice We have previously discussed the concepts of Spring AOP. One of the AOP concepts is Advice, which is further divided into various types. After (finally) is a type of advice...

3 minutes read.

Spring MVC Example – Reading HTML Form Data

We have discussed earlier a simple example of Spring MVC in which we have created multiple view pages. Here, we are going to create another Spring MVC example in which we are...

7 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 – 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 Framework Annotations

Spring Framework Annotations Spring framework provides a variety of annotations for a better approach. Using annotations, we can develop Spring applications (small & large scale) easily with less code. Following are the annotations...

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

Spring – Configuration using @Configuration

Spring – Configuration using @Configuration We have previously discussed the Spring configuration using an XML configuration file. In that case, we have defined all the bean information in the bean-configuration file (applicationContext.xml). ...

3 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 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 - Injecting values from Property Files

Spring - Injecting values from Property Files In this section, we will discuss how to inject values from a property file using the Java-based configuration. Here, we will use two annotations @Value and @PropertySource...

3 minutes read.

Spring Autowiring –Field Injection

Spring Autowiring –Field Injection The Field Injection is used to inject the fields, variables, or properties of the beans. It is not available in autowiring using XML- based configuration. It is a...

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