Object creation in Spring

Reading Time: 3 minutes

In spring IOC container deals with Object creation, Dependency satisfaction, and Life-cycle Management. It states that application programmers should only be concerned with the use of objects. Object creation and their management should be handled by an IOC container. The delegation of responsibility from the programmer to the IOC container is known as Inversion Of Control.

IOC container can create bean in two ways

  • Directly, using the default or parameterised constructor.
  • Indirectly, with the help of factory-method.

In the case of a static factory method following configuration is required.

<bean id="beanName" class="factoryClass" factory-method="methodName..." />

In case of non-static factory method following configuration is required.

<bean id="beanName" factory-bean="beanName" factory-method="methodName..." />

Note: factory-bean needs to be configured separately.

A model that creates and returns an object is known as a factory-method when beans are initialized, either by user input or from persistent data from the database.

Factory methods can be of three types.

  • Same class static factory method.
  • Different class static factory method.
  • Different class non-static factory method.

(i) Same class static factory method

public class A {  

   private A()
   {
     // private constructor   
   }   

    // same class static factory method  
    public static A getA() {  
    return new A();  
   }  
}  

Required configuration

// Required configuration for same class static factory method
<bean id="beanName" class="className" factory-method="methodName..." />

//configuration for above class
<bean id="a" class="A" factory-method="getA" />

Note: When the application code of the bean class is not available, the static factory method approach of the same class cannot be used. In such cases “different class factory method” is used as a third-party library and a single class is used.

(ii) Different class static factory method

Let’s say that the source code of class A is not available, but its object is to be created through the factory method. We can create our own factory class as follow: –

public class B {  

    // different class static factory method  
    public static A getA() {  
    return new A();  
   }  
}  

Required configuration

// Required configuration for Different class static factory method
<bean id="beanName" class="factoryClassName" factory-method="methodName..." />

//configuration for above class
<bean id="a" class="B" factory-method="getA" />

Note: If a factory method requires some input to create an object then the factory method has to be non-static. Because there is no mechanism for passing parameters in the configuration of a factory method. If the factory method is non-static, the factory object is required for their invocation at the time of the creation of the factory object. Inputs can be passed as constructor arguments.

(iii) Different class non-static factory method

Sometimes we need to pass some parameters to the factory method, in such case need to make the factory non-static. Because there is no way to pass parameters to the factory method in XML configuration. We can pass parameters to the object in the constructor. Therefore, by making the method non-static, the desired objective is achieved.

public class B {  

    // different class non-static factory method  
    public static A getA() {  
    return new A();  
   }  
   class A
    {

    }
}  

Required configuration

// To create object of class A using get() method
// Required configuration for Different class non-static factory factory method
<bean id="beanName" factory-bean="beanName" factory-method="methodName..." />

// A bean Configuration
<bean id="a" factory-bean="b" factory-method="getA" />

Reference :

https://spring.io/projects/spring-framework

Written by 

Gaurav Dubey is a Java Developer at Knoldus Software LLP. He has done M.CA from Kurukshetra University and completed Bachelor's Degree in Computer Science from M. D. University. He is a tech enthusiast with good knowledge of Java. He is majorly focused in Java practice. On the personal front, he loves to cook and travel.