Java Spring Bean Lifecycle

Spring Beans are Instantiated / Managed by Spring IoC Container.  These beans can be created by providing bean-specific configuration metadata to the container. Configuration Metadata can be provided in any of the below formats.

  1. XML
  2. Annotation
  3. Java Code

Bullet Points

  • The container will contain beans as long as they are required by Application.
  • Beans created outside the Spring container can also be registered with AC(Application Context).
  • BeanFactory is the root interface for accessing the bean container. Other interfaces are also available for a specific purpose.
  • BeanFactory is a central registry of application components(Beans).
  • These components (Beans) have lifecycle interfaces and methods which will be invoked in some order before Bean can be handed over to the application and before Bean is getting destroyed.

Bean LifeCycle :

When a bean is initialized it might require to perform some action before it can come into a usable state(State in which application can use it) and when a bean is getting destroyed, there might be some cleanup activity required for the given bean. These activities are known as bean Lifecycle.

Standard bean lifecycle interfaces & the standard order of execution are given below.
1-   IoC container will look for the configuration metadata of given Bean.
2-   Once found, the container will create the instance of Bean(Using reflection API).
3-   After instance, creation dependency will be injected(DI).

If Bean Class implements any of the below-highlighted interfaces then the corresponding method will be invoked in below order(Point 4 – 13).

 4- setBeanName method of BeanNameAware Interface. It sets the name of the bean in the bean factory that created this bean.
 5- setBeanClassLoader method of BeanClassLoaderAware Interface. Callback that supplies the bean to a bean instance.
 6- setBeanFactory method of BeanFactoryAware Interface. Callback that supplies the owning factory to a bean instance.

Below method execution will be applicable when running in an application context. (Points 7 – 11)

7- setResourceLoader  method of ResourceLoaderAware Interface. It set the ResourceLoader that this object runs in.
8- setApplicationEventPublisher  method of ApplicationEventPublisherAware Interface. Set the ApplicationEventPublisher that this object runs in.
9- setMessageSource method of MessageSourceAware Interface. Set the MessageSource that this object runs in.
10- setApplicationContext method of ApplicationContextAware Interface. Set the ApplicationContext that this object runs in.
11- setServletContext method of ServletContextAware Interface. Set the ServletContext that this object runs in.

12- postProcessBeforeInitialization method of BeanPostProcessor Interface. Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks.
13- afterPropertiesSet method of InitializingBean Interface. Invoked by a BeanFactory after it has set all bean properties supplied.

In case Bean class has custom init method defined(via init-method attribute)

14- Custom init method will be invoked.
15- postProcessAfterInitialization methods of BeanPostProcessors. Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks

When Bean Factory is getting shut down following lifecycle methods will be executed.

1- DisposableBean’s destroy method. Invoked by a BeanFactory on the destruction of a singleton.
2- Custome destroy method will be executed if there is any defined via destroy-method attributes

13 Comments Java Spring Bean Lifecycle

  1. Aakash

    “If Bean Class implements any of the below interface then corresponding method will be invoked in below order(Point 4 – 13).”

    In the above sentence you mentioned “below interface” but you have not mentioned interfaces names.

    Please make this article very clear.

    Reply
    1. J Singh

      Interfaces are mentioned in corresponding lines. To make it clear i have made some changes in article. Now interface names are highlighted in individual lines. Hope it is clear now.

      Regards

      Reply
  2. Kelvin

    I see a lot of interesting articles on your website.
    You have to spend a lot of time writing, i know how to save you a lot of work,
    there is a tool that creates unique, SEO friendly articles in couple of seconds, just
    search in google – laranita’s free content source

    Reply
  3. Revan

    Excellent tutorials but provide negotiation link(next topic link) that is very helpful to move to next topic and God bless you,,

    Reply
  4. Kristi

    You might be some good shows there. A search around the issue, and there was agreement on most of the internet log.

    Reply
  5. kurzurlaub buchen günstig

    informative and entertaining. I’ve a blog of my ‘reading’. Continue me updated!

    Reply
  6. Robbin

    naturally like your website however you need to check the spelling on several of your posts.
    Many of them are rife with spelling issues and I find it very troublesome
    to tell the truth then again I will certainly come
    back again.

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.