Using factory class to instantiate Spring IoC beans

Previous tutorial illustrate the way in which we can specify the factory method of a bean which will be used by Spring IoC container to create instance of the bean. Spring IoC also provides facility to specify a factory class of a bean and method of the factory that will be used to create the instance of the bean.

In this tutorial, we will create a factory class for our Person bean. Factory class will provide instance of Person bean to Spring IoC with some default value set to Person bean’s properties.

Tools used:

  • Spring IoC Framework 3.0.3
  • JDK 6
  • Eclipse Indigo 3.7

Our Person Bean

In this example, our Person bean is a simple pojo class with two properties : name and address.

package com.raistudies.domain;

public class Person {

    private String name;
    private String address;
    //getter and setter are removed to make the code short
    public String toString() {
        return "Name :" + + " and Address : " + this.address;

Factory Class for our Person Bean class

We have a factory class named “” that provides instance of Person bean and work through factory pattern.

package com.raistudies.domain.factory;

import com.raistudies.domain.Person;

public class PersonFactory {
    private Person person = new Person();

    public Person getPersonInstance(){
        return person;

As you can see, PersonFactory class contain instance of Person class and set set default value to property name and address and return that instance in method getPersonInstance.

Spring IoC Container configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean id="personFactory class="com.raistudies.domain.factory.PersonFactory""/>

    <bean id="person" class="com.raistudies.domain.Person"
    factory-bean="personFactory" factory-method="getPersonInstance"/>

We have to first register our factory class as a bean and then specify the factory class of Person bean using properties “factory-bean” and “factory-method”.

factory-bean: This property is used to specify the bean id which will be used as a factory class.

factory-method: Used to specify the method in factory class that will be called by the Spring IoC to get the instance of Person 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 instantiate the 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.

Run the example in eclipse indigo and you will get following output screen:

Spring IoC Bean Factory class example output

Spring IoC Bean Factory class example output

You can try the example on you pc, just download the code and lib from bellow links and import them in eclipse:

Source: Download

Source + lib: Download

Related Posts:

Leave a comment ?


  1. Thanks for your posts. They are very useful.

    I have a doubt. How would do it if Person depens on other bean which has to be injected too?


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>