There are ways to specify initialization and destruction functions

  1. The @bean annotation specifies the initialization and destruction methods
  2. The component implementation InitializingBean and DisposableBean interfaces
  3. Add @postConstruct and @PreDestroy annotations to the method

1. The @bean annotation specifies the initialization and destruction methods

  • Bean life cycle:

    1. Bean creation (constructor)
    2. Initialization (initialization function)
    3. Destroy (destroy function)
  • Initialization: The object is created and assigned, and the initialization method is called

  • Destroy: single instance: multiple instances while the container is closed: the container will not manage this bean; The container does not call the destruction method;

public class Car { 
   public Car(a){
      System.out.println("car constructor...");
   } 
   public void init(a){
      System.out.println("car ... init...");
   }   
   public void detory(a){
      System.out.println("car ... detory..."); }}Copy the code

Specifies the initialization and destruction functions

@Configuration
public class MainConfigOfLifeCycle {	
	@Bean(initMethod="init",destroyMethod="detory")
	public Car car(a){
         return newCar(); }}Copy the code

The destruction function can only be triggered if the IOC container is closed

@Test
public void test01(a){
   // create an IOC container
   AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
   System.out.println("Container created...");
   applicationContext.getBean("car");
   // Close the container
   applicationContext.close();
}
Copy the code

2. InitializingBean and DisposableBean

@Component
public class Cat implements InitializingBean.DisposableBean {
   public Cat(a){
      System.out.println("cat constructor...");
   }
   @Override
   public void destroy(a) throws Exception {
      System.out.println("cat... destroy...");
   }
   @Override
   public void afterPropertiesSet(a) throws Exception {
      System.out.println("cat... afterPropertiesSet..."); }}Copy the code

3. @ PostConstruct and @ PreDestroy

  • PostConstruct: The bean is created and the property assignment is completed; To perform the initialization method
  • PreDestroy: Notify us of the cleanup before the container destroys the bean
@Component
public class Dog  {
   public Dog(a){
      System.out.println("dog constructor...");
   }
   // called after the object is created and assigned
   @PostConstruct
   public void init(a){
      System.out.println("Dog.... @PostConstruct...");
   }  
   // Before the container removes the object
   @PreDestroy
   public void detory(a){
      System.out.println("Dog.... @PreDestroy..."); }}Copy the code

4. BeanPostProcessor [interface] : the backend processor of the bean

Execute the Bean construction process before and after the initialization function:

  1. The constructor
  2. postProcessBeforeInitialization
  3. Initialization function
  4. postProcessAfterInitialization

PostProcessBeforeInitialization: before initialization work postProcessAfterInitialization: after initialization work BeanPostProcessor configuration for all the beans are effective

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
   @Override
   public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
      System.out.println("postProcessBeforeInitialization..."+beanName+"= >"+bean);
      return bean;
   }
   @Override
   public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
      System.out.println("postProcessAfterInitialization..."+beanName+"= >"+bean);
      returnbean; }}Copy the code

The return value of this method should bea bean object