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 – 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 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 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 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 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 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 Autowiring – Setter Injection

Spring Autowiring – Setter Injection We have previously discussed an example of Setter Dependency Injection (SDI) using an XML file. In the previous case, we have defined the property name for the dependency...

2 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 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 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 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 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 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 –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 - 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 AOP - @AfterThrowing Advice

Spring AOP - @AfterThrowing Advice As we have discussed in our previous tutorial, Advice is one of the essential concepts of the Spring AOP. The after throwing advice is one of the types of...

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 – Dependency Injection

Spring – Dependency Injection One of the main features of Inversion of Control (IoC) is Dependency Injection (DI). It is a process of supplying the dependencies from one object to others. The Spring-Core container...

8 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 AOP – Pointcut Expressions

Spring AOP – Pointcut Expressions Pointcut refers to a collection of join points that specify where advice to be applied. In other words, pointcut represents a set of various join points. It defines that...

7 minutes read.