@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.
@Autowired for dependency injection in Spring Boot
@Autowired private SomeDependency someDependency;
@Autowired private UserService userService;
private final UserService userService;
@Autowired
public MyController(UserService userService) {
this.userService = userService;
}private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}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.
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(); } }
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.).
@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.
Practice
@Autowired annotation in Spring Boot?Solution
Step 1: Understand the role of @Autowired and identify the correct purpose
@Autowired is used to automatically provide the needed objects (dependencies) to a class without manual creation. It helps Spring Boot inject required components, making code cleaner and easier to maintain.Final Answer:
To automatically inject dependencies into a class -> Option DQuick Check:
@Autowired = automatic dependency injection [OK]
- Confusing @Autowired with REST endpoint annotations
- Thinking @Autowired manages database setup
- Assuming @Autowired creates new threads
@Autowired for field injection in a Spring Boot class?Solution
Step 1: Recall field injection syntax and match correct syntax
The @Autowired annotation is placed before the field declaration with its access modifier. @Autowired private Service service; correctly places @Autowired before the field type and name.Final Answer:
@Autowired private Service service; -> Option BQuick Check:
@Autowired before field = correct syntax [OK]
- Placing @Autowired after the field name
- Putting @Autowired between type and variable name
- Missing access modifier with @Autowired
myController.printServiceName() is called?
@Service
public class MyService {
public String getName() { return "ServiceA"; }
}
@RestController
public class MyController {
@Autowired
private MyService myService;
public void printServiceName() {
System.out.println(myService.getName());
}
}
Solution
Step 1: Understand @Autowired effect and trace method call output
Spring Boot injects an instance of MyService into MyController automatically. Calling getName() on injected myService returns "ServiceA" which is printed.Final Answer:
ServiceA -> Option AQuick Check:
Injected service returns "ServiceA" [OK]
- Assuming myService is null without injection
- Thinking code causes compilation error
- Expecting NullPointerException due to missing initialization
@Service
public class UserService {
private Repository repo;
@Autowired
public void setRepo(Repository repo) {
this.repo = repo;
}
}
@RestController
public class UserController {
@Autowired
private UserService userService;
public void doSomething() {
userService.repo.save();
}
}
Solution
Step 1: Check access to 'repo' field and identify cause of error
The 'repo' field in UserService is private and cannot be accessed directly from UserController. Calling userService.repo.save() causes a compilation error due to private access.Final Answer:
Field 'repo' in UserService is private and cannot be accessed directly -> Option AQuick Check:
Private fields can't be accessed outside class [OK]
- Assuming @Autowired must be on constructor only
- Thinking field injection is disallowed in UserController
- Confusing Repository annotation requirements
PaymentService into a controller using @Autowired. How can you specify which implementation to inject?Solution
Step 1: Understand multiple bean injection and use @Qualifier to specify bean
When multiple beans implement the same interface, Spring needs help to know which one to inject. Adding @Qualifier with the bean name tells Spring exactly which implementation to use.Final Answer:
Use @Qualifier annotation with the bean name along with @Autowired -> Option CQuick Check:
@Qualifier + @Autowired = select specific bean [OK]
- Trying to use @Autowired twice on one field
- Renaming classes doesn't solve injection ambiguity
- Using required=false hides errors but doesn't select bean
Ready to prove it?
Pass the test and this pattern moves unknown
