SoFunction
Updated on 2025-05-09

SpringIOC container bean initialization and destruction callback methods

Preface

The life cycle of Spring bean: init (initialization callback), destory (destroy callback). There are four ways to set the callback of the life cycle of the bean:

  • 1.@Bean specifies initialization and destruction methods
  • 2. Implement the interface
  • 3. Use JSR250
  • 4. Post-processor interface

Use scenarios:

Actively trigger the event after the bean is initialized. Such as the parameters of the configuration class.

1.@Bean specifies initialization and destruction methods

public class Phone {
 
    private String name;
 
    private int money;
    //get set
    public Phone() {
        super();
        ("Instantiate the phone");
    }
 
    public void init(){
        ("Initialization method");
    }
 
    public void destory(){
        ("Destruction Method");
    }
}
@Bean(initMethod = "init",destroyMethod = "destory")
public Phone phone(){                              
    return new Phone();                            
}     
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
();            

Printout:

Instantiate the phone
Initialization method
Destruction method

2. Implement the interface

By allowing Bean to implement InitializingBean (define initialization logic), DisposableBean (define destroy logic) interface

public class Car implements InitializingBean, DisposableBean {
 
    public void afterPropertiesSet() throws Exception {
        ("After object initialization");
    }
 
    public void destroy() throws Exception {
        ("Object Destruction");
    }
 
    public Car(){
        ("Object initialization");
    }
 
}
 

Printout:

Object initialization
After the object is initialized
Object Destruction

3. Use JSR250

By defining @PostConstruct (after object initialization) and @PreDestroy (object destruction) annotations on the method

public class Cat{
 
    public Cat(){
        ("Object Initialization");
    }
 
    @PostConstruct
    public void init(){
        ("After object initialization");
    }
 
    @PreDestroy
    public void destory(){
        ("Object Destruction");
    }
 
}

Printout:

Object Initialization
After the object is initialized
Object Destruction

4. Post-processor interface

public class Dog implements BeanPostProcessor{
 
    public Dog(){
        ("Object Initialization");
    }
 
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        (beanName+"Before object initialization");
        return bean;
    }
 
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        (beanName+"After object initialization");
        return bean;
    }
 
}

Object Initialization
Before object initialization
After the object is initialized
Before object initialization
After the object is initialized

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.