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 container. Configuration Metadata can be provided in any of below formats.

  1. XML
  2. Annotation
  3. Java Code

Bullet Points

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

Bean LifeCycle :

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

Standard bean lifecycle interfaces & there standard order of execution are given below..
1-   IoC container will look for the configuration metadata of given Bean.
2-   Once find, 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 interface then 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 destruction of a singleton.
2-   Custome destroy method will be executed if there is any defined via destroy-method attributes

By | 2017-07-21T19:41:02+00:00 June 27th, 2012|Spring|11 Comments

About the Author:


  1. Teresa December 17, 2012 at 1:32 am - Reply

    Hi there, this weekend is fastidious designed for me,
    because this occasion i am reading this fantastic
    educational post here at my house.

  2. Robbin April 13, 2013 at 3:51 am - Reply

    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.

  3. kurzurlaub buchen günstig May 8, 2013 at 4:03 am - Reply

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

  4. Kristi June 1, 2013 at 9:32 pm - Reply

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

  5. Revan October 11, 2013 at 4:40 am - Reply

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

  6. Kelvin July 6, 2014 at 8:53 pm - Reply

    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

  7. jonas January 2, 2016 at 7:27 pm - Reply

    Had very bad time reading the article

  8. Gyanendra Bhatt June 8, 2016 at 10:47 am - Reply

    BeanNameAware it’s a interface not a class

    • J Singh June 13, 2016 at 5:39 pm - Reply

      Problem rectified. Article has been updated.

  9. Aakash June 9, 2016 at 11:58 am - Reply

    “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.

    • J Singh June 13, 2016 at 5:47 pm - Reply

      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.


Leave A Comment