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.
Make sure you've completed Setting Up a Spring Boot Project and understand plain dependency injection — Spring automates exactly that idea.
💡 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.
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.
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.
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.
To ground the concept, here is the identical wiring done by hand. Spring simply automates this — you are the container in plain Java.
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.
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.