๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Autowiring in Spring


Introduction

  • Autowiring in Spring is a powerful feature that allows Spring to automatically inject dependencies into a bean without needing explicit configuration.
  • It is a part of dependency injection concept, which is central to Spring's Inversion of Control (IoC) container.

  • Note : Autowiring can't be used to inject primitive and string values. It works with reference only.

  • The main advantage of autowiring is that it :-
    • Promotes loose coupling.
    • Reduces boilerplate code.
    • Makes applications easier to maintain.

  • The main disadvantage of autowiring is that :-
    • If multiple beans of the same type are present, it can lead to ambiguity, making dependency management harder and causing runtime errors.

  • There are 3 types of Dependency Injection Mechanisms and autowiring in Spring also follows these same mechanisms to inject dependencies automatically.
    • Constructor Injection :
      • Definition : In this, dependencies are injected through the constructor of the class. Spring automatically passes the required dependencies when the bean is instantiated.
      • When to Use : We should use constructor injection when the dependencies are mandatory and must be provided during the object's creation. It's ideal for immutable objects where the dependencies cannot change after bean creation.
    • Setter Injection :
      • Definition : In this, dependencies are injected through setter methods after the bean is created. Spring calls the setter methods to inject the required dependencies.
      • When to Use : We should use use setter injection when dependencies are optional or can be set after the object is created. It is useful when dependencies might change later or when dealing with circular dependencies.
    • Field Injection (also called Direct Injection) :
      • Definition : In this, dependencies are injected directly into the fields of the class using the @Autowired annotation, without using constructors or setter methods.
      • When to Use : We should use use field injection for simple and concise code when the dependencies do not need to change and are easy to manage. It is suitable for smaller projects or cases where minimal configuration is needed.

How to achieve autowiring :

We can achieve autowiring using "autowire attribute" and "@Autowired annotation" which are explained below :-

  1. XML-based Configuration:
    • It is achieved by using the autowire attribute in the <bean> tag.
    • Click here for Autowiring in XML-based Configuration with Program.
    • Note : If multiple beans of the same type are available for dependency injection, we can use the autowire-candidate="false" attribute in the bean definition to exclude a particular bean from being considered as an autowire candidate, ensuring Spring selects the appropriate bean for injection.
  2. Java & Annotation based Configuration:
    • It is achieved using @Autowired annotation on constructors, setter methods or fields.
    • Click here for Autowiring in Java & Annotation based Configuration with Program.
    • Note : If multiple beans of the same type are available for dependency injection, then we use @Qualifier or @Primary annotations.