How to Use Dependency Injection in Spring Boot: Simple Guide
In Spring Boot, use
@Component or @Service to mark classes as beans and @Autowired to inject dependencies automatically. Spring Boot manages object creation and wiring, so you just declare dependencies in your classes.Syntax
Dependency injection in Spring Boot uses annotations to declare beans and inject them where needed.
@Componentor@Service: Marks a class as a Spring-managed bean.@Autowired: Injects the required bean into a class field, constructor, or setter.- Constructor injection is preferred for mandatory dependencies.
java
import org.springframework.stereotype.Component; import org.springframework.beans.factory.annotation.Autowired; @Component public class MyService { public String serve() { return "Service is running"; } } @Component public class MyController { private final MyService myService; @Autowired public MyController(MyService myService) { this.myService = myService; } public String process() { return myService.serve(); } }
Example
This example shows a Spring Boot application where MyService is injected into MyController using constructor injection. Spring Boot automatically creates and wires these beans.
java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Component; import org.springframework.beans.factory.annotation.Autowired; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { var context = SpringApplication.run(DemoApplication.class, args); MyController controller = context.getBean(MyController.class); System.out.println(controller.process()); } } @Component class MyService { public String serve() { return "Service is running"; } } @Component class MyController { private final MyService myService; @Autowired public MyController(MyService myService) { this.myService = myService; } public String process() { return myService.serve(); } }
Output
Service is running
Common Pitfalls
Common mistakes include:
- Not annotating classes with
@Componentor similar, so Spring does not manage them. - Using field injection instead of constructor injection, which is less testable and harder to maintain.
- Forgetting to enable component scanning or placing classes outside the scanned packages.
java
/* Wrong: Field injection (not recommended) */ @Component class WrongController { @Autowired private MyService myService; // Harder to test and maintain } /* Right: Constructor injection (recommended) */ @Component class RightController { private final MyService myService; @Autowired public RightController(MyService myService) { this.myService = myService; } }
Quick Reference
Remember these key points for dependency injection in Spring Boot:
- Use
@Component,@Service, or@Repositoryto declare beans. - Prefer constructor injection with
@Autowiredfor mandatory dependencies. - Spring Boot auto-scans packages under the main application class by default.
- Use
@Qualifierif multiple beans of the same type exist.
Key Takeaways
Annotate classes with @Component or @Service to make them Spring beans.
Use constructor injection with @Autowired for better testability and clarity.
Spring Boot automatically scans and wires beans in the main package and subpackages.
Avoid field injection as it reduces code maintainability and testability.
Use @Qualifier to resolve conflicts when multiple beans of the same type exist.