There can be situations when we want to make constructor of our class private. In these cases we often provide a factory method in our class to return its instance because we cannot create instance of such classes directly using 'new' operator. For example in the class given below, constructor is private. Therefore there is a factory method - createInstance() which returns the instance of this class.
FactoryExample.java
For such cases Spring provides an attribute 'factory-method' of <bean> tag which indicates which method of class is being used to return its instance.
ApplicationContext.xml
Note the above example is form of setter injection.
Add following class to run this example:
test.java
Output:
Constructors with arguments
The constructor of FactoryExample class above does not have any arguments. The following example shows how to pass arguments to constructor while using factory-method attribute.
FactoryExample.java
ApplicationContext.xml
Note that here instead of using:
<property name="aString" value="Hi! how are you?"/>
We have used
<constructor-arg name="s" value="Hi! how are you?"/>
So it is a form of constructor injecton.
Test.java class will remain same as in above section.
Output:
FactoryExample.java
package di.factory;
public class FactoryExample {
String aString;
//private constructor
private FactoryExample(){
}
//Injection of property using setter method.
public void setAString(String s){
aString = s;
}
public void displayString(){
System.out.println(aString);
}
//Factory Method
public static FactoryExample createInstance(){
FactoryExample fExample = new FactoryExample();
return fExample;
}
}
public class FactoryExample {
String aString;
//private constructor
private FactoryExample(){
}
//Injection of property using setter method.
public void setAString(String s){
aString = s;
}
public void displayString(){
System.out.println(aString);
}
//Factory Method
public static FactoryExample createInstance(){
FactoryExample fExample = new FactoryExample();
return fExample;
}
}
For such cases Spring provides an attribute 'factory-method' of <bean> tag which indicates which method of class is being used to return its instance.
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
<bean id="factoryExample" class="di.factory.FactoryExample" factory-method="createInstance">
<property name="aString" value="Hi! how are you?"/>
</bean>
</beans>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
<bean id="factoryExample" class="di.factory.FactoryExample" factory-method="createInstance">
<property name="aString" value="Hi! how are you?"/>
</bean>
</beans>
Note the above example is form of setter injection.
Add following class to run this example:
test.java
package di.factory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test{
public static void main(String args[]){
ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
FactoryExample obj = (FactoryExample) appContext.getBean("factoryExample");
obj.displayString();
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test{
public static void main(String args[]){
ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
FactoryExample obj = (FactoryExample) appContext.getBean("factoryExample");
obj.displayString();
}
}
Output:
Hi! how are you?
Constructors with arguments
The constructor of FactoryExample class above does not have any arguments. The following example shows how to pass arguments to constructor while using factory-method attribute.
FactoryExample.java
package di.factory;
public class FactoryExample {
String aString;
//private constructor with argument
private FactoryExample(String s){
aString = s;
}
public void displayString(){
System.out.println(aString);
}
//Factory Method
public static FactoryExample createInstance(String s){
FactoryExample fExample = new FactoryExample(s);
return fExample;
}
}
public class FactoryExample {
String aString;
//private constructor with argument
private FactoryExample(String s){
aString = s;
}
public void displayString(){
System.out.println(aString);
}
//Factory Method
public static FactoryExample createInstance(String s){
FactoryExample fExample = new FactoryExample(s);
return fExample;
}
}
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
<bean id="factoryExample" class="di.factory.FactoryExample" factory-method="createInstance">
<constructor-arg name="s" value="Hi! how are you?"/>
</bean>
</beans>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
<bean id="factoryExample" class="di.factory.FactoryExample" factory-method="createInstance">
<constructor-arg name="s" value="Hi! how are you?"/>
</bean>
</beans>
Note that here instead of using:
<property name="aString" value="Hi! how are you?"/>
We have used
<constructor-arg name="s" value="Hi! how are you?"/>
So it is a form of constructor injecton.
Test.java class will remain same as in above section.
Output:
Hi! how are you?
I would like to know your comments and if you liked the article then please share it on social networking buttons.
It’s a great post & please keep us updating like this excellent information.
ReplyDelete