Sub topic 5
Initialization callbacks
This chapter assumes you know core Java annotations, basic Spring dependency injection (@Component, @Bean, @Configuration), and how to run a Spring application. It covers the three ways to run code after Spring has built a bean.
Why initialization callbacks exist
A constructor runs before Spring has injected field or setter dependencies. Code that needs a fully wired bean, such as opening a connection or validating configuration, belongs in an initialization callback. Spring calls it once, after injection and before the bean is handed to other beans.
Three ways to hook in
| Mechanism | Where you declare it | Couples code to Spring? |
|---|---|---|
| `@PostConstruct` | Annotation on a method | No, it is a Jakarta standard |
| `InitializingBean.afterPropertiesSet()` | Implement an interface | Yes |
| `@Bean(initMethod = "...")` | Name a method in the `@Bean` definition | No |
@PostConstruct is the usual choice for your own classes. Use initMethod for third-party classes you cannot annotate.
Example: all callbacks on one bean
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class DemoBean implements InitializingBean, DisposableBean {
public DemoBean() {
System.out.println("constructor");
}
@PostConstruct
void postConstruct() {
System.out.println("@PostConstruct");
}
@Override
public void afterPropertiesSet() {
System.out.println("afterPropertiesSet");
}
@PreDestroy
void preDestroy() {
System.out.println("@PreDestroy");
}
@Override
public void destroy() {
System.out.println("DisposableBean.destroy");
}
}At startup this prints, in order:
constructor@PostConstructafterPropertiesSet
If the bean also declares a custom initMethod, it runs last. Destruction callbacks are covered in a later chapter.
Registering an init method on a third-party class
When you cannot add annotations, name the methods in @Bean.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PoolConfig {
@Bean(initMethod = "open", destroyMethod = "close")
public Pool pool() {
return new Pool();
}
}
class Pool {
public void open() {
System.out.println("pool opened");
}
public void close() {
System.out.println("pool closed");
}
}The init method must take no arguments. Spring calls it after injection, in the same slot as the other callbacks.
Common errors
- Using injected fields in the constructor. With field or setter injection they are still
nullthere. Move the logic to@PostConstruct, or use constructor injection. - Expecting a callback to run on a method that takes arguments.
@PostConstructandinitMethodmethods must have no parameters. Spring rejects a@PostConstructmethod that has any. - Doing slow work in init. A long or failing callback delays startup. An exception here fails the whole context, because the bean never becomes available.
- Relying on
@PostConstructwithout annotation processing. In a plain Spring context, it works withAnnotationConfigApplicationContextand Spring Boot. Both register the processor that handles it. - Calling a proxied method from init. Transactional or other proxy-based behavior is usually not applied yet, because proxies are created after initialization.
Exercise
A bean has a constructor, a @PostConstruct method, and an afterPropertiesSet method. In what order do they run, and why can't the constructor use an @Autowired field?
Show answer
The order is constructor, @PostConstruct, then afterPropertiesSet. Spring creates the object with the constructor first and injects field and setter dependencies afterward, so an @Autowired field is still null inside the constructor.