Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Bean life cycle in spring

This example gives you an idea on how to Initialize bean in the program and also explains the lifecycle of bean in spring.


Spring 4 Introduction and example Features of Spring Framework 5 How to make Spring web Login form? How to make Simple form in Spring web? Spring, Hibernate login and registration application Spring Framework Tutorial for beginners with examples Spring Framework for Apache Hadoop 2.3.0 GA released Spring Framework 4.1 GA is released with major features Why to use Spring Framework? Spring Framework 4.1 - First Release candidate available Spring IO Platform 1.0.0 Released Spring 4: Login Form using Spring MVC and Hibernate Example Spring 4 MVC Login form Example with source code Spring 4 MVC Hello World Example: Spring 4 MVC Tutorial will full source code Spring Web MVC Application Error:ClassNotFoundException: DispatcherServlet on deploying Features of Spring 4 Spring Framework 4.0 released Spring Framework 4: Spring Framework 4 Tutorials and Example Spring Integration 3.0 Release is released and available for download Spring Tutorial for Beginners Java Springs Framework Tutorial Spring Architecture Spring Framework Tutorials database spring registration form Spring Login Example Roseindia Spring Tutorial Spring Tutorial Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration

Bean life cycle in spring


This example gives you an idea on how to Initialize bean in the program and also explains the lifecycle of bean in spring. Run the given bean example to retrieves the values of the bean using java file. Here in the file given below i.e. (context.xml) we have declare the bean definition.

<bean id="Mybean" class="Bean">   
   <property name="company" value="Name"/>   <property name="value" value=""/>

Here "Bean" is the name of the bean class which would be further referred in the xml file with the id "MyBean".

<property name="company" value="Name"/>:-Declares  the property name of the bean and its value.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

  <bean id="Mybean" class="Bean">
  <property name="company" value="Name"/>
  <property name="value" value=""/> 

Here is the file named through which we are retrieving the properties of the bean which we have defined in the above file i.e. context.xml

 XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml")):-Here we are creating an instance of the XmlBeanFactory which is used to read bean definition from an XML document

new ClassPathResource("context.xml"):-
Creates a new ClassPathResource for ClassLoader .Here the context.xml is the file which is to be loaded.

class BeanSupport implements InitializingBean :-Here the InitializingBean  interface is implemented by bean class. The use of this interface here is  to do some post processing actions when all the properties have been set by the Bean Factory..

public String toString() {   return String.format("%s : \"%s\"",, getValue());   

Here the method toString() is overridden which is returning the the company name and value that has been defined in the context.xml file.

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;

public class Main {

  public static void main(String[] argsthrows Exception {
 XmlBeanFactory factory = new XmlBeanFactory(new 

class Bean extends BeanSupport {

  private String company;
  public void setCompany(String company) { = company;
  public String toString() {
  return String.format("%s : \"%s\"", getValue());
class BeanSupport implements InitializingBean {

  private String value;

  public final void afterPropertiesSet() throws Exception {

  public final void setValue(String value) {
  this.value = value;

  protected final String getValue() {
  return this.value;

Output of the program

Nov 25, 2008 5:25:41 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [context.xml]
Name : ""
BUILD SUCCESSFUL (total time: 1 second)

Download Source code


Share on Google+Share on Google+

Bean life cycle in spring

Posted on: May 24, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Bean life cycle in spring   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Steve White
April 17, 2011
libraries for build, run

build: /usr/share/java/spring-core.jar:/usr/share/java/spring-beans.jar run: .:/usr/share/java/spring-core.jar:/usr/share/java/spring-beans.jar:/usr/share/java/commons-logging.jar