DEV Community

Elayaraj C
Elayaraj C

Posted on

Understanding Annotations, Beans, Spring Container & Dependency Injection in Spring Boot

  • What annotations are
  • What beans are
  • What the Spring container is
  • What dependency injection is
  • Why, when, how to use all of them

Understanding Annotations, Beans, Spring Container & Dependency Injection in Spring Boot

🔍 Introduction

If you're learning Spring or Spring Boot, you’ll constantly hear terms like:

  • @Component, @Autowired, @bean
  • Spring container
  • Dependency Injection (DI)
  • Beans And how do they help make your application clean, modular, and powerful?

** What are Annotations in Spring?**

In Spring, annotations are special markers (starting with @) that tell the Spring framework to do certain things automatically.

Common Annotations:

What is a Bean?

In Spring, a Bean is just a Java object managed by the Spring container.

For example:

This MyService class becomes a Spring Bean because it’s annotated with @Component.

What is the Spring Container?

The Spring container is the core of the Spring Framework.

** It is responsible for:**

  • Creating objects (beans)
  • Managing their lifecycle
  • Injecting dependencies into them
  • Configuring them based on annotations or XML

Think of the container as the brain that runs your Spring app.

How does it work?

When your app starts:

  • Spring scans your classes (like @Component, @Service)
  • Creates and stores them in memory
  • Connects everything together automatically

What is Dependency Injection (DI)?

Dependency Injection means that an object’s dependencies are provided (injected) from the outside, instead of the object creating them itself.

Spring takes care of creating the object and injecting it where needed.

Why Use Dependency Injection?

  • Loose coupling – makes code easier to test and maintain
  • Better modularity
  • Cleaner code with fewer new keywords

How Spring Does Dependency Injection

Spring supports 3 types of DI:

Spring will automatically create the Engine bean and inject it into Car.

When and Why to Use Spring Annotations & DI?

Top comments (0)