1. The first step is to implement a Bean, also known as new

  2. The second step, IOC dependency injection, is to configure the instantiated beans according to the Spring context, known as IOC injection

  3. Third, the setBeanName implementation, if the Bean already implements the BeanNameAware interface, calls its implementation’s setBeanName (String) method, which passes in the Bean ID value in the Spring configuration file

  4. Step 4: The BeanFactoryAware implementation. If the Bean already implements the BeanFactoryAware interface, it calls its implementation setBeanFactory. SetBeanFactory (BeanFactory) passes the Spring factory itself (you can get other beans this way by simply configuring a normal Bean in the Spring configuration file)

  5. In step 5, the ApplicationContextAware implementation, if the Bean already implements the ApplicationContextAware interface, calls the setApplicationContext (ApplicationContext) method, Passing in the Spring context (this also implements step 4, but is better because ApplicationContext is a subinterface to the BeanFactory and has more implementations)

  6. Step 6, postProcessAfterInitialization interface implementation – initializes the pretreatment, if the Bean associated the BeanPostProcessor interface, Will call postProcessBeforeInitialization (Object obj, String s) method, the BeanPostProcessor is often used as Bean content changes, and as a result of this is at the end of the call that the Bean is initialized, the method of Memory or caching techniques can also be applied.

  7. Step 7, init-method. If the Bean has init-method data configured in its Spring configuration file, it automatically calls its configured initialization method.

  8. Step 8 postProcessAfterInitialization, if the Bean associated the BeanPostProcessor interface, will call postProcessAfterInitialization (Object obj. String s) method.

  9. Ninth, the Destroy expiration automatic cleanup stage, which takes place when the Bean is no longer needed. If the Bean implements the interface DisposableBean, its implementation Destroy () method will be called;

  10. In step 10, destroy-method is automatically configured for cleanup, and finally, if the Bean’s Spring configuration has the destroy-method property configured, its configuration destruction method is automatically called

  11. In step 11, the bean tag has two important properties (init-method and destroy-method) with which you can customize the initialization and deactivation methods. They are also annotated (@postConstruct and @Predestroy).

Conclusion:

  1. Instantiate the Bean
  2. Setting property values (dependency injection)
  3. If the BeanNameAware interface is implemented, call setBeanName to set the ID or Name of the Bean
  4. If the BeanFactoryAware interface is implemented, call setBeanFactory to set the BeanFactory.
  5. If the ApplicationContextAware interface is implemented, call setApplicationContext to set the application-Context.
  6. Call BeanPostProcessor’s pre-initialization method.
  7. Call the afterProprocessSet () method of the InitializingBean
  8. Call the custom init-method method
  9. Call the post-initialization method of BeanPostProcessor.
  10. Use our Bean

Spring destroys beans as follows:

  1. Call Destroy () to DisposableBean
  2. Call the custom destroy-method method