Calling Bean using init() method in Spring, this section describes  the way to initialize a bean through its lifecycle events using the init() method .Here we have defined the property and values of  the bean using the init method as shown below:-

<bean id=”mybean” class=”Bean” init-method=”init”>:-Here “Bean” is the name of the bean class which would be referred in the xml file with the id “mybean”.

init-method=”init”:-Specify the init method named “init” in the configuration file.

name=”name”> the <property> element is used to declare the attributes and to pass the desired value to the property element, the <value> element is used. Here the property name is “name”and its value is “”.



mybean” class=”Bean” init-method=”init”>
<property name=”name”>
<property name=”address”>
<property name=”type”>   <value>Software Development Company</value>


  <bean id="mybean" class="Bean" init-method="init">
  <property name="name">
  <property name="address">
  <property name="type">
  <value>Software Development Company</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. initMethod.xml.

BeanFactory factory = new XmlBeanFactory(new FileSystemResource(“initMethod.xml”)):-Creates an instance of the XmlBeanFactory which is used to read bean definition from an XML document.

import org.springframework.beans.factory.BeanCreationException;

import org.springframework.beans.factory.BeanFactory;

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


public class Bean {

  private static final String DEFAULT_NAME = "Girish";

  private String name = null;

  private String address = null;

  private String type = null;


  public void setName(String name) { = name;


  public void setAddress(String address) {

  this.address = address;


  public void setType(String type) {

  this.type = type;


  public void init() {

  System.out.println("Initializing bean");

  if (name == null) {

  System.out.println("Using default name");

  name = DEFAULT_NAME;


  if (address == null) {

  System.out.println("Using default name");

  address = DEFAULT_NAME;


  if (type == null) {

  System.out.println("Using default name");

  type = DEFAULT_NAME;




  public String toString() {

  return "Name: " + name + "nAddress: " + address + "nType: " + type ;


  public static void main(String[] args) {

  BeanFactory factory = new XmlBeanFactory(new FileSystemResource(


  Bean Bean1 = getBean("mybean", factory);


  private static Bean getBean(String beanName, BeanFactory factory) {

  try {

  Bean bean = (Bean) factory.getBean(beanName);


  return bean;

  } catch (BeanCreationException ex) {

  System.out.println("An error occured

in bean configuration: " + ex.getMessage());

  return null;




Output of the program

Initializing beanName:

Address: Rohini

Type: Software Development Company

Download Source code

Leave a Reply

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