Thursday 10, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 9 March 2014

Spring Bean Life Cycle

In this tutorial we are going to see the Spring Bean life Cycle.

When Bean instantiated, it may required to perform some initialization to get it into a usable state.When the bean no longer required and removed from container some clean up may be required.

For above mention operation some list of activities take place behind the scenes.

Spring Lifecycle Callbacks

To interact with the container's management of the bean lifecycle, you can implement the Spring InitializingBean and DisposableBean interfaces. The container calls afterPropertiesSet() for the former and destroy() for the latter to allow the bean to perform certain actions upon initialization and destruction of your beans.

Internally, the Spring Framework uses BeanPostProcessor implementations to process any callback interfaces it can find and call the appropriate methods. If you need custom features or other lifecycle behavior Spring does not offer out-of-the-box, you can implement a BeanPostProcessor yourself.

In addition to the initialization and destruction callbacks, Spring-managed objects may also implement the Lifecycle interface so that those objects can participate in the startup and shutdown process as driven by the container's own lifecycle.

The JSR-250 @PostConstruct and @PreDestroy annotations are generally considered best practice for receiving lifecycle callbacks in a modern Spring application. Using these annotations means that your beans are not coupled to Spring specific interfaces.


If you don't want to use the JSR-250 annotations but you are still looking to remove coupling consider the use of init-method and destroy-method object definition metadata.

We will see the following life cycle callback event in upcoming chapter.

  1. Initialization callbacks
  2. Destruction callbacks
  3. Default initialization and destroy methods
  4. @PostConstruct
  5. @PreDestroy
  6. Startup and Shutdown callbacks

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Spring DI - Injecting Inner BeansIn this tutorial we are going to see about Spring Injecting Inner Beans .Inner BeanA <bean/> element inside the <property/> or <constructor-arg/> elements defines a so-called inner bean. An inner bean defi… Read More
  • Spring Bean Scope In this tutorial we are going to see the overview of Spring bean scope. When you create bean definition in Spring, you can declare the scope of bean. You can control not only the various dependencies and configuration value… Read More
  • Spring singleton Bean ScopeIn this tutorial we are going to see Spring singleton Bean scope with example program.Only one shared instance of a singleton bean is managed, and all requests for beans with an id or ids matching that bean definition result … Read More
  • Spring DI - Setter-based Dependency InjectionIn this tutorial we are going to see about Spring setter based dependency injection with example program,Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument const… Read More
  • Spring DI – Constructor Argument ResolutionIn this tutorial we are going to see about Spring Constructor based Dependency Injection argument resolutions with example program.Constructor argument resolution matching occurs using the argument's type. If no potential amb… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Spring Bean Life Cycle Rating: 5 Reviewed By: eHowToNow