Dependency Injection in Spring Boot: The Basics

Dependency Injection (DI) is a design pattern where objects are provided with their dependencies instead of creating them directly.

It’s like saying:

“Hey, I don’t want to build this thing myself—just give me a ready-made one that I can use.”

Dependency Injection in Spring Boot: The Basics

🧠 Why Is It Important?

Without DI:

With DI:

✅ Benefits of DI:

  • Cleaner code
  • Better testability (you can mock dependencies)
  • Encourages separation of concerns
  • Makes code easier to manage and scale

🌱 How Dependency Injection in Spring Boot works

Spring Boot automatically injects beans using:

  • @Autowired
  • Constructor injection
  • Field injection
  • Setter injection (less common)

Spring does this by:

  1. Scanning your classes for annotations like @Component, @Service, @Repository, etc.
  2. Creating instances of these classes (called beans)
  3. Injecting them into other beans where required

🛠️ 1. Using @Component to Define a Bean

Let’s say you have a simple service class:

Now Spring knows to manage EmailService as a bean.

🔁 2. Injecting a Dependency Using @Autowired

Here, Spring automatically injects an instance of EmailService into NotificationService.

💡 Best Practice: Constructor Injection

Instead of using @Autowired on fields, use constructor injection:

✅ Advantages:

  • Easier to test
  • Makes immutability possible
  • Encouraged by modern Spring practices

📘 Note: Since Spring 4.3+, if a class has only one constructor, Spring automatically injects dependencies — no need for @Autowired.

💾 Example: Full Working Application

EmailService.java

NotificationService.java

MyController.java

Visit: http://localhost:8080/send

✅ You should see:

🔐 What Happens Behind the Scenes?

  1. Spring Boot starts and scans your classes (@ComponentScan)
  2. It detects all @Component, @Service, @Repository, etc.
  3. It creates and manages their instances (beans)
  4. It injects dependencies where required using reflection

🚫 Common Mistakes to Avoid

MistakeIssue
Forgetting to annotate a classSpring won’t recognize it as a bean
Using field injection without @AutowiredDependency will be null
Trying to inject a bean that’s not scannedInjection fails with NoSuchBeanDefinitionException
Circular dependenciesCan lead to startup errors

📘 FAQ

❓ Do I always need @Autowired?

Not if you’re using constructor injection and the class has only one constructor. Spring will inject automatically.

❓ Can I inject one service into multiple components?

Yes! Spring manages beans as singletons by default (unless specified otherwise), so you can reuse them.

❓ What if I have multiple implementations of a dependency?

Use @Qualifier to specify which one to inject.

📚 External References

✅ Summary

ConceptDescription
DI (Dependency Injection)Providing dependencies from outside
@ComponentMarks a class as a Spring bean
@AutowiredInjects a dependency
Constructor InjectionRecommended, testable, and cleaner
Spring ContainerManages beans and performs the injection

Spring Boot makes dependency injection effortless, letting you focus on your logic while it wires up everything behind the scenes.