How to Use @Autowired in Spring Framework

1 min read

How to Use @Autowired in Spring Framework

In Spring, the @Autowired annotation is one of the most powerful and widely used features for dependency injection. It allows Spring to resolve and inject collaborating beans into your application components automatically, without needing to instantiate them manually.


What is @Autowired?

@Autowired is an annotation provided by Spring that allows automatic injection of beans by type.

  • Defined in: org.springframework.beans.factory.annotation.Autowired
  • Works in:
    • Constructors
    • Setters
    • Fields

Types of Dependency Injection with @Autowired

TypeDescription
FieldDirectly inject into a field (common but less testable)
SetterInject via setter method
ConstructorPreferred for immutability and testability

Example Project: Using @Autowired in All 3 Ways

Let’s build a simple Spring Boot application with a GreetingService that will be injected into a GreetingController.

1. Create Spring Boot Project

Use Spring Initializr or create manually with the following dependencies:

  • Spring Boot Starter Web

2. Maven pom.xml (simplified)

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Step-by-Step Code Implementation

GreetingService.java

package com.example.demo.service;

import org.springframework.stereotype.Service;

@Service
public class GreetingService {
    public String greet(String name) {
        return "Hello, " + name + "!";
    }
}
  • @Service tells Spring to manage this class as a bean.

GreetingController.java (3 Injection Styles)

package com.example.demo.controller;

import com.example.demo.service.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/greet")
public class GreetingController {

    private GreetingService greetingService;

    // ✅ Constructor Injection (Preferred)
    @Autowired
    public GreetingController(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    // ✅ Field Injection (Less testable)
    // @Autowired
    // private GreetingService greetingService;

    // ✅ Setter Injection (Optional)
    // @Autowired
    // public void setGreetingService(GreetingService greetingService) {
    //     this.greetingService = greetingService;
    // }

    @GetMapping("/{name}")
    public String greet(@PathVariable String name) {
        return greetingService.greet(name);
    }
}

Test the API

Start the application and hit:

http://localhost:8080/greet/Alice

Output:

Hello, Alice!

Optional: Handle Multiple Beans with @Qualifier

If you have multiple beans of the same type, use @Qualifier:

@Service("friendlyService")
public class FriendlyGreetingService implements GreetingService {
    public String greet(String name) {
        return "Hey buddy, " + name + "!";
    }
}

@Autowired
@Qualifier("friendlyService")
private GreetingService greetingService;

Notes and Best Practices

PracticeRecommendation
Prefer constructor injectionBetter for immutability, testability, and clarity
Avoid field injectionDifficult to unit test without reflection
Use @Qualifier when neededIf multiple beans implement same interface
Required=true by defaultAdd @Autowired(required=false) to make optional

Common Errors

ErrorCauseSolution
No qualifying beanBean not scanned or definedAnnotate with @Component, @Service, etc.
NullPointerExceptionField not injectedUse correct injection method or check config

Summary

FeatureSupport
Field Injectionyes
Setter Injectionyes
Constructor Injectionyes (Recommended)
Optional Beansyes via @Autowired(required = false)
Multiple Beansyes via @Qualifier

Final Directory Structure

src/
└── main/
    └── java/
        └── com/example/demo/
            ├── DemoApplication.java
            ├── controller/
            │   └── GreetingController.java
            └── service/
                └── GreetingService.java

🤞 Never miss a story from us, get weekly updates to your inbox!

Leave a Reply

Your email address will not be published. Required fields are marked *