Using self contain factory method to instantiate Spring IoC beans

Spring IoC provides a lot of options using which we can handle dependency injection. It also provided facility to define factory method of a bean that will be used to get instance of a bean. The factory method can be self contain in bean class or it can be in other factory class of the bean.

In this tutorial, we will see how to define self contain factory method of a bean to create bean instances. As we know by default all the beans in Spring IoC container managed are Singleton but in this example we will handle the Singleton characteristic of bean at bean level. Our Person bean will be work as Singleton and we will define factory method in Spring IoC configuration file.

Singleton bean class with self contain factory method

package com.raistudies.domain;

public class Person {

    private String name;
    private String address;
    private static Person person;

    private Person(){

    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 synchronized static Person getInstance(){
        if (person == null)
            person = new Person();
        return person;
    public String toString() {
        return "Name :" + + " and Address : " + this.address;

    protected Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();

We have made our Person bean as Singleton by defining a static method getInstance() to get the instance of the class, specifying private access modifier to constructor of the bean and by overriding the clone method. Now we will configure this Singleton class in Spring IoC.

Configuring self contain factory method to instantiate Spring IoC beans

Following is the configuration file of Spring IoC container to configure our bean class:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean id="person" class="com.raistudies.domain.Person" factory-method="getInstance">
        <property name="name" value="Rahul Mondal"/>
        <property name="address" value="Jaipur, India"/>

We have used factory-method attribute to specify static factory method of the bean that will provide instance of the bean to Spring IoC container.

Run the example to test the output

Let us run the example and find out how it works. We have made a runner class to test the output. Runner class creates an instance of BeanFactory by loading out bean configuration file using ClassPathXmlApplicationContext class.

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);

After that the runner get the instance of Person bean from BeanFactory and print the value of Person bean.

While running the example from eclipse, I get the following output screen:

Using initialization method to initialize a Spring IoC Bean

Using initialization method to initialize a Spring IoC Bean

You can try the example by downloading the example code + lib from following link:

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>