Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring - Autowiring

In Spring, autowiring allows developers to automatically inject dependencies without specifying them explicitly. This is done by the Spring container based on certain conventions or metadata.

When defining beans in the XML configuration or using Java-based configuration, the autowire functionality helps reduce the amount of configuration needed by automatically resolving bean references.

There are several modes of autowiring:

1. no (default):

This is the default setting, which means no autowiring. You must specify bean references manually using the <property> or <constructor-arg> elements in XML configuration.

<bean id="someBean" class="com.example.SomeBean" autowire="no">
    <!-- Explicitly defined dependencies here -->
</bean>

2. byType:

Autowiring by datatype. Spring looks for a bean with a matching data type in the container. If there's exactly one bean of the matching type, it will inject it. If none or multiple candidates are found, an exception will be thrown.

<bean id="someBean" class="com.example.SomeBean" autowire="byType"/>

3. byName:

Autowiring by bean name. Spring will look for a bean with a name matching the property or variable name that needs to be autowired.

<bean id="someBean" class="com.example.SomeBean" autowire="byName"/>

4. constructor:

Similar to byType, but it applies to constructor arguments. If there's a constructor whose arguments can all be satisfied by matching the types of beans in the container, it will be used for injection.

<bean id="someBean" class="com.example.SomeBean" autowire="constructor"/>

5. autodetect:

Spring first tries to autowire by constructor, and if that fails, it tries by type.

Annotation-based Autowiring:

Spring also provides annotation-based autowiring:

  1. @Autowired: It can be applied to variables, setters, or constructors. Spring will try to resolve the dependency by type.

    @Autowired
    private SomeBean someBean;
    
  2. @Qualifier: Used along with @Autowired to specify the name of the bean when multiple beans of the same type exist.

    @Autowired
    @Qualifier("specificBeanName")
    private SomeBean someBean;
    
  3. @Resource: Similar to @Autowired, but it does injection by name. If a name is not specified, it defaults to the field name.

    @Resource(name="specificBeanName")
    private SomeBean someBean;
    

Advantages of Autowiring:

  1. Reduced Configuration: Requires less explicit wiring in the configuration file or code.

  2. Less Error-Prone: Reduces the chances of mistakes when specifying bean dependencies.

Disadvantages of Autowiring:

  1. Clarity: The configuration might be harder to understand since dependencies aren't explicitly defined.

  2. Potential Ambiguity: If there are multiple beans of the same type, you might run into exceptions or unexpected behavior unless you handle such situations carefully.

In general, developers should use autowiring judiciously and ensure that the advantages outweigh the potential challenges in their specific context.

  1. Spring Autowiring example:

    • XML Configuration:
      <bean id="car" class="com.example.Car" autowire="byType">
      </bean>
      
    • Java Configuration:
      @Configuration
      public class AppConfig {
          @Bean
          @Autowired
          public Car car() {
              return new Car();
          }
      }
      
  2. Autowiring in Spring with annotations:

    • Use @Autowired annotation for autowiring:
      @Autowired
      private Engine engine;
      
  3. Constructor Autowiring vs. Setter Autowiring in Spring:

    • Constructor Autowiring:
      @Autowired
      public Car(Engine engine) {
          this.engine = engine;
      }
      
    • Setter Autowiring:
      @Autowired
      public void setEngine(Engine engine) {
          this.engine = engine;
      }
      
  4. Autowired annotation in Spring:

    • Use @Autowired for automatic dependency injection:
      @Autowired
      private Engine engine;
      
  5. Spring @Qualifier with Autowiring:

    • Use @Qualifier to specify which bean to inject:
      @Autowired
      @Qualifier("v8Engine")
      private Engine engine;
      
  6. Dependency Injection in Spring with Autowiring:

    • Example with Constructor Autowiring:
      @Service
      public class CarService {
          private final CarRepository carRepository;
      
          @Autowired
          public CarService(CarRepository carRepository) {
              this.carRepository = carRepository;
          }
      }