09 · Spring Framework and the IoC container / LESSON 37 OF 102
Inversion of control and ApplicationContext
Inversion of control transfers object assembly to a container while your classes keep their business behavior.
Before this lesson: DAO boundaries and connection pools
Understand the topic
Dependency injection is one way to achieve IoC: collaborators arrive through constructors instead of being located or created inside business methods. ApplicationContext registers bean definitions, creates instances, resolves dependencies, and provides lifecycle services.
A bean is an object managed by that context. Creating the same class with new outside the context does not automatically apply container injection, lifecycle callbacks, or proxies. Keep the domain usable without starting Spring when possible.
EXAMPLE · JAVA
ContainerDemo.java
import org.springframework.context.annotation.*;
public class ContainerDemo {
@Configuration static class Config {
@Bean String greeting() { return "Container ready"; }
}
public static void main(String[] args) {
try (var context = new AnnotationConfigApplicationContext(Config.class)) {
System.out.println(context.getBean(String.class));
}
}
}What to expect
With spring-context on the classpath, prints Container ready without starting a web server or Spring Boot.
YOUR TURN
Put it into practice
- 01Start a plain AnnotationConfigApplicationContext.
- 02Register a formatter and a service that receives it through a constructor.
- 03Verify: The service works both when assembled manually and when retrieved from the container.
Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.
UNDER THE HOOD
Build the mental model
Inversion of control transfers object assembly to a container while your classes keep their business behavior.
- 01Dependency injection is one way to achieve IoC: collaborators arrive through constructors instead of being located or created inside business methods. ApplicationContext registers bean definitions, creates instances, resolves dependencies, and provides lifecycle services.
- 02A bean is an object managed by that context. Creating the same class with new outside the context does not automatically apply container injection, lifecycle callbacks, or proxies. Keep the domain usable without starting Spring when possible.
- 03Trace the example: With spring-context on the classpath, prints Container ready without starting a web server or Spring Boot.
A mistake worth avoiding
A manually constructed service does not receive framework behavior.
DEBUGGING ROOM
Something broke. Where do you look?
A manually constructed service does not receive framework behavior.
Before revealing the diagnosis, write down your first investigation step.
CHECK YOUR UNDERSTANDING
Is every Java object automatically a Spring bean?
BUILD IT YOURSELF
Start a plain AnnotationConfigApplicationContext.
- 1.Start a plain AnnotationConfigApplicationContext.
- 2.Register a formatter and a service that receives it through a constructor.
- 3.Reproduce this failure, then repair it: A manually constructed service does not receive framework behavior.
Acceptance checklist
Lab completion is your self-report. Knowledge checks do not certify project quality.
RETRIEVE IT FROM MEMORY
Is every Java object automatically a Spring bean?
Revisit this lesson in three days after a successful check or lab.
Ready for the next step?
The service works both when assembled manually and when retrieved from the container.
Reading is separate from passing a knowledge check or completing a lab.