trendingzones

JAVA + SPRING AI — INTRODUCTION

What Is Dependency Injection? Spring’s Core Idea, Explained

Almost everything Spring does — the annotations, the container, the “magic” wiring — exists to support one idea: an object shouldn’t build the things it depends on. It should just say what it needs, and let something else hand it over.

The Quick Answer

Dependency injection (DI) means an object declares what it needs to do its job, instead of creating those things itself inside its own code. In plain Java, a class that needs a payment processor might just write new StripePaymentProcessor() right there in its constructor. With dependency injection, the class instead says “I need something that can process payments,” and something outside the class — in Spring’s case, the framework itself — decides which actual implementation to hand it. The class never has to know or care.

The Restaurant Kitchen Analogy

Imagine a chef whose recipe says: “take the tomatoes that were prepped this morning, and use them.” The chef doesn’t grow the tomatoes, doesn’t drive to the farm, and doesn’t even decide which farm they came from. The chef just declares “I need tomatoes,” and the kitchen’s prep station has already made sure some are sitting there, ready to use.

Now picture the opposite: a chef whose recipe says “drive to Farmer Joe’s field, pick the tomatoes yourself, wash them, then cook.” That recipe now depends on one specific farm. If Farmer Joe’s field floods, or the restaurant wants to switch to a different, better supplier, someone has to rewrite the recipe itself. Worse, if you just want to practicethe cooking technique without a real trip to a real farm, you can’t — the recipe forces the farm trip every single time.

That second chef is what a class looks like when it constructs its own dependencies inline. The first chef — who just declares a need and trusts the kitchen to supply it — is what dependency injection looks like.

Why “Building It Yourself” Is a Trap

It feels natural, at first, for an object to just build what it needs. But it creates two very real, very practical problems once an application grows past a toy example:

Declaring a Need vs. Building It Yourself

The shift dependency injection asks for is small to describe and large in effect: stop writing newfor things your class depends on, and instead accept them as inputs — usually through the class’s constructor. The class becomes simpler, since it’s no longer responsible for knowing how to build its dependencies, only for using them. And deciding which concrete implementation to hand over moves to one central place, instead of being scattered across every class that needs it.

That “one central place” is exactly the job Spring’s container takes on. The Intermediate level covers what that container actually is, and the exact mechanics of how a Spring class receives what it needs.

Fun Fact

Dependency injection isn’t a Spring invention — it’s a specific case of a broader principle called “Inversion of Control” (IoC), a term that predates Spring itself. Spring’s own documentation describes its core container as an “IoC container” precisely because dependency injection is its way of implementing that older, more general idea.

Test Yourself

What problem does dependency injection solve?

In the kitchen analogy, what does the "injected" ingredient represent?

Ready for the actual Spring mechanics — what a “bean” is, what theApplicationContext does, and the real, idiomatic way to write a constructor-injected Spring class? Continue to the Intermediate level →