Spring Framework

Spring Projects

Spring Project 1

Creation of an Object in Spring Framework
Previous Home Next

Introduction :In the Spring Framework provide the concept IOC container which are responsible for creation of an object, satisfied the dependency and lifecycle management. First task of the IOC container is creation of an objects. Objects can be create through constructor and factory method.

By default IOC container create object using constructor. In order to facilitate object creation using factory method. Information of the factory need to be provided.

Type of Object Creation

In the spring framework concept IOC container which are create an object following two types as below:

  1. Using Constructor
  2. Using Factory Method
Object Creation Using Constructor

IOC container create object through the constructor, If we are creating object through IOC container first you are declare parameterize constructor in our bean class and define method to print any message through the configuration file. The following syntax used in configuration file if we are create object through the constructor.


<bean id="hello" class="org.r4r.Hello">
<constructor-arg value="Hello World !"/>
</bean>

Object Creation Using factory method

IOC container create object through the factory method, If we are creating object through IOC container first you are declare parameterize method in our bean class and print any message through the configuration file. The following syntax used in configuration file if we are create object through the factory method.


<bean id="number" class="org.r4r.Complex" factory-method="getNumber" scope="prototype"/>

 

Factory method can be divided in following three ways:

Same class static factory


class A{
  private(){
//Logic is written
}
public static A getA(){
return new A();
}
}

Following configuration need to provided to the IOC container to use getA() factory method.

Different class static factory


class B{
{
==
}
class A{  //Different class static factory
public static B getB(){
return new B();
}
}

Following configuration need to provided the IOC container to use getB() factory method.


<bean id="b" class="A" factory-method="getB"/>

Different class non-static factory


class B{
{
==
}
class A{  //Different class nonstatic factory
public B getB(){
return new B();
}
}

Following configuration need to provide the IOC container to use getB() factory method.


<bean id="a" class="A"/>
<bean id="b" class="B" factory-method="getB" factory-bean="a"/>

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: