Bean life cycle in springboot

Bean life cycle in springboot

Today I mainly share the process of the life cycle of Bean in Springboot. If there is any shortage, please correct me.

Bean life cycle process

Bean life cycle generally has the following four stages:

  • Bean definition

  • Bean initialization

  • Bean lifetime

  • Bean destruction

Bean definition process:

  1. The first step, resource positioning, is that Spring finds the corresponding class according to our defined annotation (@Component).

  2. When the resource is found, it starts to parse and save the defined information. At this time, the bean is not initialized, which needs attention.

  3. Then publish the definition of the bean to the SpringIoc container. At this time, there is no bean generation in the SpringIoc container. Just defined information.

Bean initialization

After the definition and initialization of the Bean, SPring will continue to complete the instantiation and dependency injection of the Bean, so that a dependency injection completed Bean can be obtained from the IoC container. The following diagram is an example of an initialization diagram:

Spring initialization bean

Bean life cycle

Bean life cycle in Spring

Test the life cycle of Bean through code

Join the life cycle interface

   BeanNameAware,BeanFactoryAware,ApplicationContextAware,INitiali
   zingBean,DisposableBean这几个接口, 并实现里面的方法

Code

Environment: jdk 1.8 springboot2.2 idea

Through the custom class Children, to implement the interface to be called by Bean initialization, have a deeper understanding of the process of Bean life cycle.

  1. Define the interface Person class and Furit class

       package chen.beanprocessor.model;
    
       public interface Person {
        
       void service();
      
        // 设置水果类型
        void eatFruit(Fruit fruit);
        }  
    
       /**
        * @Author Chen
        * @Date 2020/4/18 17:04
        **/
        public interface Fruit {
        void use();
        }
    
  2. Define Person and Fruit implementation classes Children and Apple, and inject the Apple class into Children, and add the life cycle implementation interface to Children

      /**
       * @Author Chen
       * @Date 2020/4/18 17:07
       * 水果实现类Apple
       **/
       @Component
       public class Apple implements Fruit {
       @Override
       public void use() {
       System.out.println(this.getClass().getSimpleName()+"这个苹果很甜");
       }
       }
    
     @Component
     public class Children implements Person, BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
    
     Fruit fruit = null;
    
     // 注入水果类
     public Children(@Autowired Fruit fruit){
      this.fruit = fruit;
     }
     @Override
     public void service() {
     fruit.use();
     }
    
     @Override
     public void eatFruit(Fruit fruit) {
     this.fruit = fruit;
     }
    
    
     @Override
      public void setBeanFactory(BeanFactory beanFactory) throws BeansException   {
      System.out.println("this"+this.getClass().getSimpleName()+"调用BeanFactory的setBeanFactory方法");
      }
    
      @Override
      public void setBeanName(String name) {
      System.out.println("this"+this.getClass().getSimpleName()+"调用setBeanName的方法");
      }
    
      @Override
      public void destroy() throws Exception {
      System.out.println("this"+this.getClass().getSimpleName()+"调用DisposableBean的方法");
      }
    
      @Override
       public void afterPropertiesSet() throws Exception {
       System.out.println("this"+this.getClass().getSimpleName()+"调用Initializing的afterPropertiesSet()的方法");
       }
    
     @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    System.out.println("this"+this.getClass().getSimpleName()+"调用Application的setApplicationContext的方法");
     }
    
    @PostConstruct
    public void init(){
    
     System.out.println(this.getClass().getSimpleName()+"注解@PostConstruct的自定义的初始化方法");
    }
    
     @PreDestroy
    public void destory1(){
    System.out.println(this.getClass().getSimpleName()+"调用@dPrDestory的自定义销毁的方法");
    }
    
    }
    

3. Define the test class

      @SpringBootApplication
     public class BeanprocessorApplication {

     public static void main(String[] args) {
     SpringApplication.run(BeanprocessorApplication.class, args);
     AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
     Person person = applicationContext.getBean(Children.class);
     System.out.println("===========初始化完成==========");
     person.service();
     applicationContext.close();
     }
    }

Test Results

 thisChildren调用setBeanName的方法
 thisChildren调用BeanFactory的setBeanFactory方法
 thisChildren调用Application的setApplicationContext的方法
 Children注解@PostConstruct的自定义的初始化方法
 thisChildren调用Initializing的afterPropertiesSet()的方法

 2020-04-18 17:28:38.902  INFO 9784 --- [           main] c.b.BeanprocessorApplication             : Started BeanprocessorApplication in 0.682 seconds (JVM running for 1.29)
 thisChildren调用setBeanName的方法
 thisChildren调用BeanFactory的setBeanFactory方法
 thisChildren调用Application的setApplicationContext的方法
 Children注解@PostConstruct的自定义的初始化方法
 thisChildren调用Initializing的afterPropertiesSet()的方法

 ===========初始化完成==========
 Apple这个苹果很甜


 Children调用@dPrDestory的自定义销毁的方法
 thisChildren调用DisposableBean的方法
 Children调用@dPrDestory的自定义销毁的方法
 thisChildren调用DisposableBean的方法

The test results can clearly see the life cycle of the bean. From the test results, the Bean is initialized twice, this is because when the Children class is initialized, the injected Apple class is also initialized.

Code word is not easy, please like it

Code word is not easy, please like it

Code word is not easy, please like it

Only after tracing the source can we move forward

Reference materials:

"In-depth introduction to SpringBoot" Yang Kaizhen

Guess you like

Origin www.cnblogs.com/chentang/p/12727943.html