Monday, March 21, 2011

Spring : Life Cycle of bean

Beans are managed by IOC container, having a life cycle associated with it. These are the possible ways of managing there life cycle:

  • Callback API implementation
  • XML configuration
  • Annotations

There are two distinct spring containers one is bean factory and another is application context. Life cycle phases varies a lil in the containers. More precisely, only one additional phase is added in case of application context. Let's see what these phases are:

  1. Instantiate: in this phase container finds the bean's definition and default constructor called.
  2. Autowiring executes
  3. Dependency check performed
  4. setters method of bean called
  5. setBeanFactory () / setApplicationContext () / method called
  6. afterPropertiesSet() / init-method /@PostConstruct method called
  7. Application running, beans ready to work
  8. destroy()/ destroy-method / @PreDestroy method called
    An existing bean can be removed from the container in two ways:
    1. DisposableBean: If bean implements the DisposableBean interface then destroy() method is called.
    2. Call-custom destroy: if custom-destroy method is specified then it is called.

Implementing the life cycle interface

No comments:

Post a Comment