IoC Container & Dependency Injection
The ApplicationContext is Spring's IoC container: it discovers your beans , builds the dependency graph, and injects collaborators for you — usually via constructor injection .
Learn IoC Container & Dependency Injection in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…
Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Before You Start
Make sure you've completed Setting Up a Spring Boot Project and understand plain dependency injection — Spring automates exactly that idea.
What You'll Learn
The Big Idea — A Stage Manager Assembling the Cast
💡 Analogy: The IoC container is a stage manager . Each actor (bean) declares which props and co-stars they need; the stage manager fetches everyone, arranges them in the right order, and hands each actor exactly what they require before the show. The actors never go looking for their own props — control is inverted to the manager.
Your job is just to declare the cast ( @Component ) and their needs (constructor parameters); Spring directs the assembly.
1️⃣ Declaring Beans with @Component
Annotating a class with @Component (or a stereotype like @Service ) registers it as a bean during component scanning. Spring then knows it can create and inject it.
2️⃣ Constructor Injection
Declare the dependency as a final field and accept it in the constructor. Spring supplies the matching bean — and with a single constructor, @Autowired is optional.
3️⃣ How the Container Builds the Graph
At startup the container resolves dependencies and constructs beans in the correct order, injecting each one's collaborators. By default beans are singletons — one shared instance per container.
4️⃣ The Same Idea in Plain Java
To ground the concept, here is the identical wiring done by hand. Spring simply automates this — you are the container in plain Java.
🧠 Quick Recall
Answer: ApplicationContext — it instantiates, wires and manages the lifecycle of beans.
Answer: when a bean has a single constructor — Spring uses it automatically (since 4.3).
Answer: singleton — exactly one shared instance per container.
Common Errors
- ❌ Forgetting @Component/@Service. The class isn't a bean and can't be injected. Fix: annotate it (or declare an @Bean).
- ❌ Field injection. Hides dependencies and breaks plain tests. Fix: use constructor injection.
- ❌ Two beans, one type. Causes NoUniqueBeanDefinitionException. Fix: add @Primary or @Qualifier.
- ❌ new-ing a managed bean yourself. Bypasses the container and its injection. Fix: let Spring create it.
- ❌ Bean outside the scanned packages. Never discovered. Fix: keep beans under the main class's package.
Pro Tips
- 💡 Always constructor-inject and mark fields final for explicit, immutable beans.
- 💡 Use the right stereotype — @Service for logic, @Repository for data, @Controller for web — for clearer intent.
- 💡 Program to interfaces so the container can inject any implementation.
- 💡 Drop @Autowired on single-constructor beans to reduce noise.
📋 Quick Reference
Concept
Syntax / idea
Note
container
ApplicationContext
The IoC container
bean
@Component
Managed object
service bean
@Service
Stereotype of @Component
inject
constructor parameter
Preferred form
@Autowired
optional on 1 ctor
Inferred since 4.3
default scope
singleton
One per container
ambiguity
@Primary / @Qualifier
Disambiguate
📚 Resources / Keep Learning
- 📘 The IoC Container — Spring Framework docs
- 📗 Dependency Injection — Spring Framework docs
- 📗 Building an Application with Spring Boot — spring.io
❓ Frequently Asked Questions
🎉 Lesson Complete!
You now understand the ApplicationContext, what a bean is, how @Component and its stereotypes get discovered by scanning, how constructor injection works (and when @Autowired is optional), and how the container assembles the whole dependency graph.
Next up: Beans, Scopes & Configuration — @Bean, @Configuration, scopes and resolving ambiguity.
Practice quiz
What is the Spring IoC container also known as?
- The compiler
- The ApplicationContext
- The classloader
- The garbage collector
Answer: The ApplicationContext. The ApplicationContext is Spring's IoC container: it instantiates, configures, wires and manages the lifecycle of beans.
What is a 'bean' in Spring?
- A primitive type
- An object instantiated, assembled and managed by the Spring container
- A SQL table
- A configuration file
Answer: An object instantiated, assembled and managed by the Spring container. A bean is simply an object whose creation and wiring is handled by the Spring IoC container.
What does @Component do?
- Marks a class so component scanning registers it as a bean
- Makes a class abstract
- Starts a new thread
- Deletes a bean
Answer: Marks a class so component scanning registers it as a bean. @Component flags a class for component scanning, so Spring detects it and registers it as a managed bean.
Which injection style does Spring recommend?
- Field injection
- Setter injection
- Static injection
- Constructor injection
Answer: Constructor injection. Constructor injection is recommended: dependencies are explicit, can be final, and the bean is always fully initialised.
On a class with a single constructor, @Autowired is...
- Required or it fails
- Optional — Spring infers it
- Forbidden
- Only for fields
Answer: Optional — Spring infers it. Since Spring 4.3, if a bean has exactly one constructor, @Autowired is optional; the container uses it automatically.
What does the container do with the dependency graph at startup?
- Ignores it
- Builds it: creates beans and injects collaborators in the right order
- Stores it in a database
- Prints it only
Answer: Builds it: creates beans and injects collaborators in the right order. Spring resolves dependencies and constructs the object graph, injecting each bean's collaborators as it builds them.
By default, how many instances of a singleton-scoped bean exist per container?
- One per request
- One per thread
- Exactly one, shared
- One per method call
Answer: Exactly one, shared. The default 'singleton' scope means a single shared instance is created per Spring container.
Why is field injection discouraged?
- It is faster but unsafe
- It hides dependencies and makes plain-Java testing harder
- It cannot compile
- It only works in Kotlin
Answer: It hides dependencies and makes plain-Java testing harder. Field injection hides what a class needs and prevents final fields, complicating testing without the container.
Which annotations are specialised forms of @Component?
- @Override and @Deprecated
- @Service, @Repository and @Controller
- @Test and @BeforeEach
- @Entity and @Table
Answer: @Service, @Repository and @Controller. @Service, @Repository and @Controller are stereotype annotations meta-annotated with @Component for clearer intent.
What happens if Spring finds two beans matching a single dependency type?
- It picks at random silently
- It throws an error unless you disambiguate (e.g. @Qualifier/@Primary)
- It merges them
- It ignores both
Answer: It throws an error unless you disambiguate (e.g. @Qualifier/@Primary). An ambiguous dependency causes a NoUniqueBeanDefinitionException unless you mark a @Primary or use @Qualifier.
Continue this course
- Previous: Setting Up a Spring Boot Project
- Next: Beans, Scopes & Configuration