Using initialization method to initialize a Spring IoC Bean

With the previous tutorial, you know the process of registering a bean class with Spring IoC container using xml configuration files. In this tutorial we will see how to define an initialization method of bean to Spring IoC, so that Spring IoC container invokes the method after creating instance of the bean to perform some initialization work.

Tools Used:

  • Spring IoC Framework 3.0.3

In our example, we will define an initialization method of our Person class that will initialize the properties name and address of the Person class as value “Unknown”.

Our Spring IoC Bean class with Initialization method

package com.raistudies.domain;

public class Person {
    private String name;
    private String address;

    public void init(){ = "Unknown";
        this.address = "Unknown";
    public String getAddress() {
        return address;
    public void setAddress(String address) {
        this.address = address;
    public String getName() {
        return name;
    public void setName(String name) { = name;

    public String toString() {
        return "Name :" + + " and Address : " + this.address;

As you can see, the init() method works as a initialization method for Person bean class to initialize it’s properties with value “Unknown”.

Spring IoC Container configuration file

Now, we have to tell Spring IoC about the init() method, so that it can invoke it after creating the instance of  bean.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean id="person" class="com.raistudies.domain.Person" init-method="init"/>

init-metthod attribute of <bean/> tag is used for specifying the initialization method of bean.

Run the example to test the output

package com.raistudies.runner;

import org.springframework.beans.factory.BeanFactory;

import com.raistudies.domain.Person;

public class SpringBeanTestRunner {

    public static void main(String[] args) {
        BeanFactory factory = new ClassPathXmlApplicationContext("app-context.xml");
        Person person = (Person)factory.getBean("person");
        System.out.println("Person detail is : " + person);

Our test runner class will create the instance of BeanFactory using ClassPathXmlApplicationContext and load the configuration file, after that it will get the instance of Person bean from BeanFactory and print the Person bean on console.

While running the example in ellipse, you will get following result, which justify that initialization method has bean called by Spring IoC container.

Using initialization method to initialize a Spring IoC Bean

Using initialization method to initialize a Spring IoC Bean

You can download source or source with lib of the above example from following links:

Source: Download

Source + lib: Download

Related Posts:

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>