Sub topic 1
The container and what it manages
Prerequisites
This chapter assumes you know core Java, including interfaces and annotations. It also assumes you have used @Component, @Autowired, @Configuration and @Bean, and that you have built and run a Spring or Spring Boot application. Later chapters build on the ideas introduced here.
What the container does
The Spring IoC container is the part of Spring that creates your objects, wires them together and controls how long they live. IoC means inversion of control. Your code does not call new on its collaborators. It declares what it needs, and the container supplies it.
An object that the container creates and manages is called a bean. A plain object you create with new yourself is not a bean, even if its class is annotated with @Component. The container does not know about it, so it gets no injection and no callbacks.
BeanFactory and ApplicationContext
Spring exposes the container through two interfaces.
| Interface | Role |
|---|---|
| `BeanFactory` | The basic container. It creates beans and injects dependencies. |
| `ApplicationContext` | Extends `BeanFactory`. It adds event publishing, message sources, resource loading and automatic registration of post-processors. |
In application code you almost always use an ApplicationContext. Spring Boot creates one for you when SpringApplication.run starts. In a plain Java program you create one yourself, for example AnnotationConfigApplicationContext.
What the container manages
For each bean, the container is responsible for four things:
- Reading its definition, which says which class to use and how to build it.
- Creating the instance and injecting its dependencies.
- Running lifecycle callbacks after creation and before destruction.
- Deciding how many instances exist and how long each lives. This is the bean's scope.
The rest of this tutorial covers the last three in detail. The first one, the bean definition, is the starting point.
A first look at a running context
The following program starts a context, asks it for a bean and closes it. Closing the context matters, because that is when the container destroys its singleton beans.
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
@ComponentScan
class AppConfig {
}
@Component
class Greeter {
String greet() {
return "hello from a bean";
}
}
public class Main {
public static void main(String[] args) {
try (AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class)) {
Greeter greeter = ctx.getBean(Greeter.class);
System.out.println(greeter.greet());
}
}
}@ComponentScan without arguments scans the package of AppConfig, so Greeter is found and registered. Calling getBean is only for demonstration. In real code you inject beans instead of looking them up.
How the pieces connect
Common mistakes
- Creating a bean with
newand expecting injection. The container only injects into objects it creates. - Forgetting to close a plain
ApplicationContext. Destruction callbacks on singletons then never run. - Using
BeanFactorydirectly in application code. You lose features thatApplicationContextprovides.
Exercise
Why does an object created with new Greeter() inside a method not receive dependencies or lifecycle callbacks, even if Greeter is annotated with @Component?
Show answer
The container only manages objects it creates from bean definitions. The annotation marks the class for registration, but an instance made with new is outside the container, so Spring never injects into it or calls its callbacks.