Thursday 13 December 2012

Initializing a Spring IoC Bean

Posted by Naveen Katiyar On 21:37 3 comments

 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.naveen.domain;

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

    public void init(){
        this.name = "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) {
        this.name = name;
    }

    public String toString() {
        return "Name :" + this.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="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id="person" class="com.naveen.domain.Person" init-method="init"/>
</beans>

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

Run the example to test the output


package com.naveen.runner;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.naveen.domain.Person;

public class SpringBeanTestRunner {

    public static void main(String[] args) {
        BeanFactory factory = new ClassPathXmlApplicationContext("servlet-config.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.

Download Source code


3 comments:

Anonymous said...

Great information. Lucky me I came across your website by chance (stumbleupon).
I have book marked it for later!

Here is my homepage - Psn Code Generator

Anonymous said...

Have you ever considered writing an ebook or guest authoring on other websites?
I have a blog based upon on the same information you discuss
and would really like to have you share some stories/information.
I know my subscribers would value your work. If you are even remotely interested, feel free to shoot me an email.


Have a look at my blog post :: pirater Un compte facebook

Anonymous said...

My relatives every time say that I am killing my time here at
web, however I know I am getting experience everyday by reading such pleasant articles.



My web-site: dragon city hack