Sub topic 4
The phases of a bean's life
This chapter assumes you know core Java, basic Spring dependency injection (@Component, @Autowired, @Bean), and how to run a Spring or Spring Boot application. It gives the overview. Later chapters cover each callback in detail.
Why phases matter
Spring does not simply call new on your class. The container (the ApplicationContext) moves each bean through a fixed sequence of steps. Knowing the sequence tells you where it is safe to use injected dependencies, where to start resources, and where to release them.
The phases
A singleton bean goes through these phases:
- Definition. The container reads configuration, such as component scanning and
@Beanmethods. For each bean it builds aBeanDefinition, which is metadata: class, scope, dependencies, and init and destroy method names. No object exists yet. - Instantiation. The container calls the constructor, or the factory method for a
@Bean. - Population. The container injects dependencies into fields and setters. Constructor arguments were already resolved in step 2.
- Awareness. If the bean implements interfaces such as
BeanNameAwareorApplicationContextAware, the container passes it the matching infrastructure object. - Pre-initialization. Each
BeanPostProcessorruns itspostProcessBeforeInitializationmethod. - Initialization. The container runs
@PostConstruct, thenafterPropertiesSet(), then any custominitMethod. - Post-initialization. Each
BeanPostProcessorrunspostProcessAfterInitialization. This is where Spring may replace the bean with a proxy. - In use. The bean is ready and other beans can receive it.
- Destruction. When the context closes, the container runs
@PreDestroy, thendestroy(), then any customdestroyMethod.
Seeing it happen
Start a context in a plain Java program and close it with try-with-resources. Closing the context is what triggers destruction.
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
class AppConfig {
}
public class Main {
public static void main(String[] args) {
try (AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class)) {
System.out.println("context running");
}
}
}Any singleton found by the scan is created before context running prints. Its destroy callbacks run after the try block ends.
Common errors
- Using a dependency in the constructor of a field-injected bean. With field or setter injection, the fields are still
nullduring the constructor. Use constructor injection, or move the logic to@PostConstruct, which runs after population. - Expecting a shutdown callback in a program that never closes the context. If the JVM exits without closing the context,
@PreDestroydoes not run. Spring Boot registers a shutdown hook for you. In plain Spring you callclose()orregisterShutdownHook(). - Assuming every bean gets destruction callbacks. The container manages the full lifecycle only for singletons. Prototype beans are created and handed over, and the container then forgets them. The prototype scope chapter explains why.
Exercise
A bean uses field injection with @Autowired private Repo repo;. Its constructor calls repo.findAll() and throws a NullPointerException. In which phase is the dependency set, and where could the call move to?
Show answer
Dependencies are injected in the population phase, which comes after instantiation, so repo is still null in the constructor. Move the call to a @PostConstruct method, which runs in the initialization phase, or switch to constructor injection.