Saturday, March 26, 2016

Spring Bean Life cycle

Life cycle of an instance from a bean

By ApplicationContext


  1. Create an instance
  2. Set property of the instance
  3. *BeanNameAware(Interface) implement to call setBeanName, pass you BeanName
  4. *BeanFactoryAware(Interface) implement to  call setBeanFactory, pass you BeanFactory
  5. *ApplicationContextAware(Interface) implement to  call, pass you ApplicationContext
  6. *BeanPostProcessor(Interface) postProcessBeforeInitialization(function), any one or more Bean class to implement to call, like filter, remember to return the object. AOP
  7. *InitializingBean(Interface) implement to call afterPropertiesSet
  8. *customized function defined by "init-method" in <bean> config, or @PostConstruct
  9. *BeanPostProcessor(Interface) postProcessAfterInitialization(function)
  10. Instance Ready to use
  11. Spring Container closing
  12. *DisposableBean(Interface) implement to call destroy
  13. *customized function defined by "destroy-method" in <bean> config, or @PreDestroy

Most common steps: 1,2,6, 9,10,11

* steps: optional
Suggested to use customized function since it's not bonded to Spring

By BeanFactory

Do NOT have ApplicationContext and BeanPostProcessor
Only have 1,2,3,4,7,8,10,11,12,13





No comments:

Post a Comment