What is @Autowired Annotation in Spring?
@Autowired is one of the most commonly used annotations in the Spring Framework.
It is used for:
Automatic Dependency Injection in Spring applications.
In simple words:
Spring automatically creates and injects required objects instead of developers creating them manually using new.
Why @Autowired is Needed
In Java applications, classes often depend on other classes.
Example:
- Controller depends on Service
- Service depends on Repository
- Repository depends on Database
Without Spring
Developers manually create objects.
Example
public class StudentController {
StudentService service = new StudentService();
}
Problems with Manual Object Creation
- Tight coupling
- Hard maintenance
- Difficult testing
- Poor scalability
Spring Solution
Spring Framework provides:
Dependency Injection (DI)
using:
@Autowired
What is Dependency Injection?
Dependency Injection means:
Spring automatically provides required dependent objects to a class.
Simple Real-Time Example
Suppose:
- StudentController needs StudentService
Instead of:
new StudentService()
Spring automatically injects the object.
Architecture Example
Client Request
|
v
Controller
|
v
Service
|
v
Repository
|
v
Database
Example Without @Autowired
public class StudentController {
StudentService service =
new StudentService();
}
Problems
- Controller tightly coupled with service
- Hard to replace implementation
- Testing becomes difficult
Example With @Autowired
@RestController
public class StudentController {
@Autowired
private StudentService service;
}
What Happens Internally?
Spring:
- Creates StudentService object
- Stores object in Spring Container
- Injects object automatically into controller
Spring Container Role
Spring Container manages:
- Object creation
- Object lifecycle
- Dependency injection
Spring Container Flow
Spring Container
|
--------------------------------
| |
v v
Creates Beans Injects Dependencies
What is a Bean?
A Bean is an object managed by Spring Container.
Example Bean
@Service
public class StudentService {
}
Types of Dependency Injection in Spring
- Field Injection
- Constructor Injection
- Setter Injection
1. Field Injection
Dependencies are injected directly into fields.
Example
@RestController
public class StudentController {
@Autowired
private StudentService service;
}
Advantages
- Simple
- Easy to write
Disadvantages
- Hard to test
- Not recommended for production
2. Constructor Injection
Dependencies are injected using constructor.
Recommended Approach
Constructor injection is considered best practice.
Example
@RestController
public class StudentController {
private final StudentService service;
@Autowired
public StudentController(
StudentService service
) {
this.service = service;
}
}
Advantages
- Immutable dependencies
- Better testing
- Cleaner design
- Recommended in Spring Boot
3. Setter Injection
Dependencies are injected using setter methods.
Example
@RestController
public class StudentController {
private StudentService service;
@Autowired
public void setService(
StudentService service
) {
this.service = service;
}
}
Advantages
- Optional dependencies possible
Disadvantages
- Dependencies can change later
Common Spring Stereotype Annotations
| Annotation | Purpose |
|---|---|
| @Component | General Spring Bean |
| @Service | Business logic layer |
| @Repository | Database layer |
| @Controller | Spring MVC Controller |
| @RestController | REST API Controller |
Example Full Flow
Repository Layer
@Repository
public class StudentRepository {
}
Service Layer
@Service
public class StudentService {
@Autowired
private StudentRepository repository;
}
Controller Layer
@RestController
public class StudentController {
@Autowired
private StudentService service;
}
How Spring Finds Dependencies
Spring scans classes using:
Component Scanning
Process
- Spring scans packages
- Detects annotations like @Service
- Creates beans automatically
- Injects dependencies using @Autowired
Autowired By Type
By default:
Spring injects dependency by type.
Example
@Autowired private StudentService service;
Spring searches:
StudentService Bean
Problem: Multiple Beans
Suppose:
OnlinePaymentService CardPaymentService
Both implement:
PaymentService
Error
NoUniqueBeanDefinitionException
Solution Using @Qualifier
@Autowired
@Qualifier("onlinePaymentService")
private PaymentService service;
Autowired Optional Dependency
Optional dependencies can be configured.
Example
@Autowired(required = false) private StudentService service;
What Happens If Bean Not Found?
Spring throws:
NoSuchBeanDefinitionException
Advantages of @Autowired
- Reduces boilerplate code
- Loose coupling
- Improves maintainability
- Easy testing
- Cleaner architecture
Disadvantages of Field Injection
- Hard unit testing
- Hidden dependencies
- Less immutable design
Best Practice
Use:
Constructor Injection
because:
- Better immutability
- Easier testing
- Cleaner design
Real-Time Example in Microservices
Suppose Interview Service needs Repository.
Flow
Controller
|
v
Service
|
v
Repository
|
v
MySQL Database
Example
@Service
public class InterviewService {
private final InterviewRepository repository;
@Autowired
public InterviewService(
InterviewRepository repository
) {
this.repository = repository;
}
}
@Autowired in Spring Boot
In Spring Boot:
- @Autowired is heavily used
- Automatic configuration simplifies dependency injection
Modern Spring Boot Recommendation
In modern Spring Boot:
- @Autowired on constructor is optional if only one constructor exists
Example
@Service
public class StudentService {
private final Repository repository;
public StudentService(
Repository repository
) {
this.repository = repository;
}
}
Advantages in Microservices
- Loose coupling between layers
- Easier service management
- Better modularity
- Supports scalable architecture
Real-Time Company Usage
Companies using Spring Boot microservices such as:
- Amazon
- Netflix
- Uber
- Banking systems
- E-Commerce platforms
heavily use dependency injection and Spring-managed beans.
Professional Interview Answer
@Autowired is a Spring annotation used for automatic dependency injection. It allows Spring Container to automatically create and inject required bean objects into classes instead of manually creating objects using the new keyword. @Autowired supports field injection, constructor injection, and setter injection. Constructor injection is considered best practice because it improves immutability, testability, and maintainability. @Autowired helps achieve loose coupling, cleaner architecture, and easier dependency management in Spring and Spring Boot applications.
Why Interviewers Like This Answer
- Explains dependency injection clearly
- Covers Spring internals
- Includes real-time examples
- Explains best practices
- Shows production-level Spring knowledge
Frequently Asked Questions
What is @Autowired used for?
It is used for automatic dependency injection in Spring.
What is dependency injection?
Dependency injection means Spring automatically provides required objects to classes.
Which injection type is recommended?
Constructor injection is recommended.
What happens if multiple beans exist?
Spring throws NoUniqueBeanDefinitionException unless @Qualifier is used.
What happens if bean is not found?
Spring throws NoSuchBeanDefinitionException.