Skip to content

Spring Bean Lifecycle

How Spring Manage Bean

ApplicationContext is an interface in Spring that provides configuration information to an application. It's the central interface in a Spring application. ApplicationContext is responsible for instantiating, configuring, and assembling beans. It reads configuration metadata to determine what objects to instantiate, configure, and assemble.

ApplicationContext provides basic features, including:

  • Publishing events to registered listeners

  • Methods for accessing application components

  • Internationalization support

  • Loading file resources

ApplicationContext is read-only while the application is running, but it can be reloaded if the implementation supports it. ApplicationContext is different from BeanFactory. BeanFactory creates a bean object when the getBean() method is called. ApplicationContext loads all the beans and creates objects at startup.

Lifecycle

From the perspective of ApllicationContext,

  • Spring reads the bean configuration and metadata, by creating BeanDefinitionObject for each bean

    The bean configuration can be from XML or Annotation

  • Spring executes all the custom BeanFactoryPostProcessor hooks to modify a bean definition

    In AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization, it will executes all the BeanFactoryPostProcessor hook one by one.

  • After the bean definitions are set up, Spring will executes the following steps for each bean

    1. Create a bean using its bean definition
    2. Set the propeties and the dependencies of the Bean
    3. Executes the BeanPostProcessor hook to allow modify the bean
    4. Executes the PostConstruct method defined in the bean, it's done by BeanPostProcessor

      In InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization and InitDestroyAnnotationBeanPostProcessor.invokeInitMethods, it executes the post init methods.

      PostConstruct is a J2EE standard annotation. 5. Executes the afterPropertiesSet method in a bean, it's done by BeanPostProcessor

      afterPropertiesSet is Spring way to post init a bean

    5. Executes custom bean initialization method, it's done by BeanPostProcessor

      Bean initialization methods can be specified either in the value of the init- method attribute in the corresponding element in a Spring XML configuration or in the initMethod property of the @Bean annotation.

      The method will be only invoked once.

  • When the Spring application context is to shut down, the beans will be destoried in the following order

    1. Executes the PreDestroy method

    2. Executes destroy method that implementing the DisposableBean interface

      The method will be only invoked once.

    3. Executes custom bean destory method

      Bean destruction methods can be specified either in the value of the destroy-method attribute in the corresponding element in a Spring XML configuration or in the destroyMethod property of the @Bean annotation.

      Destory method will be only execueted once