@Autowired helps your Spring Boot app automatically connect parts that depend on each other, so you don't have to create or manage them manually.
0
0
@Autowired for dependency injection in Spring Boot
Introduction
When you want Spring to provide an object your class needs without creating it yourself.
When you have a service class that needs to use a repository class.
When you want to keep your code clean and easy to test by letting Spring handle object creation.
When you want to avoid writing boilerplate code to create and pass dependencies.
When you want to easily swap implementations without changing the dependent class.
Syntax
Spring Boot
@Autowired private SomeDependency someDependency;
Place @Autowired above a field, constructor, or setter method to tell Spring to inject the needed object.
Spring looks for a matching bean (object) by type and injects it automatically.
Examples
Injects UserService into the current class automatically.
Spring Boot
@Autowired private UserService userService;
Constructor injection: Spring injects UserService when creating MyController.
Spring Boot
private final UserService userService;
@Autowired
public MyController(UserService userService) {
this.userService = userService;
}Setter injection: Spring calls this method to inject UserService.
Spring Boot
private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}Sample Program
This Spring Boot app has two components: MessageService and MessagePrinter. MessagePrinter uses @Autowired to get MessageService automatically. When the app runs, it prints the message from MessageService.
Spring Boot
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @Component class MessageService { public String getMessage() { return "Hello from MessageService!"; } } @Component class MessagePrinter { @Autowired private MessageService messageService; public void printMessage() { System.out.println(messageService.getMessage()); } } @SpringBootApplication public class DemoApplication implements CommandLineRunner { @Autowired private MessagePrinter printer; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... args) throws Exception { printer.printMessage(); } }
OutputSuccess
Important Notes
If Spring finds more than one matching bean, it will throw an error unless you specify which one to use.
Using constructor injection is recommended for better testability and immutability.
@Autowired works only on Spring-managed beans (classes annotated with @Component, @Service, @Repository, etc.).
Summary
@Autowired automatically connects dependent parts in Spring Boot.
You can use it on fields, constructors, or setters.
It helps keep your code clean and easy to maintain.