A simple example to demonstrate, how to provide constructor initialization to a bean using constructor-arg tag in configuration file of Spring Framework
-------- Scenario 1 ----------
1. Create a normal class having constructor say 'Dog' -
2. Create a spring.xml file placed in classpath of your application -
3. Create a Test class for the application (imp) -
Output of the Scenario 1 :
-------- Scenario 2 ----------
If Scenario 2 in Dog.java is un-commented than the Spring cannot identify which constructor to call, so it goes by calling Dog(String , String) constructor and prints the following output :
Output of the Scenario 2 :
-------- Scenario 3 ----------
If Scenario 1 is run with following spring.xml changes below -
Output of the Scenario 3 :
Scenario 1 and Scenario 3, have only one change instead of providing value as 20 we have provided "twenty" a String value. Spring cannot convert "twenty" to int and throws java.lang.NumberFormatException. Think of it as Spring cannot perform something like Integer.parseInt("String") .
-------- Scenario 1 ----------
1. Create a normal class having constructor say 'Dog' -
package com.hubberspot.spring;
public class Dog {
private String breed;
private int teeth;
// Scenario 1 : Overloaded Constructor
public Dog(String breed, int teeth){
this.breed = breed;
this.teeth = teeth;
}
/* Scenario 2 : both strings
public Dog(String breed, String teeth){
this.breed = breed;
//this.teeth = teeth;
}*/
// Generating getters and setters
// for all the properties of Dog
public int getTeeth() {
return teeth;
}
public void setTeeth(int teeth) {
this.teeth = teeth;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public void move() {
System.out.println("A Dog of breed "+ getBreed() +", " +
"having "+ getTeeth() +" teeth moves slowly ...");
}
}
2. Create a spring.xml file placed in classpath of your application -
|
3. Create a Test class for the application (imp) -
package com.hubberspot.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class WildLifeApplication {
public static void main(String[] args) {
// ApplicationContext is a Spring interface which
// provides with the configuration for an application.
// It provides us with all the methods that BeanFactory
// provides. It loads the file resources in a older
// and generic manner. It helps us to publish events to the
// listener registered to it. It also provides quick support
// for internationalization. It provides us with the object
// requested, it reads the configuration file and provides
// us with the necessary object required.
// We are using concrete implementation of ApplicationContext
// here called as ClassPathXmlApplicationContext because this
// bean factory reads the xml file placed in the classpath of
// our application. We provide ClassPathXmlApplicationContext
// with a configuration file called as spring.xml placed
// at classpath of our application.
ApplicationContext context =
new ClassPathXmlApplicationContext(("spring.xml"));
// In order to get a object instantiated for a particular bean
// we call getBean() method of ClassPathXmlApplicationContext
// passing it the id for which the object is to be needed.
// Here getBean() returns an Object. We need to cast it back
// to the Dog object. Without implementing new keyword we
// have injected object of Dog just by reading an xml
// configuration file.
Dog dog = (Dog)context.getBean("dog");
// Calling our functionality
dog.move();
}
}
Output of the Scenario 1 :
-------- Scenario 2 ----------
If Scenario 2 in Dog.java is un-commented than the Spring cannot identify which constructor to call, so it goes by calling Dog(String , String) constructor and prints the following output :
Output of the Scenario 2 :
-------- Scenario 3 ----------
If Scenario 1 is run with following spring.xml changes below -
<bean id="dog" class="com.hubberspot.spring.Dog"> <constructor-arg value="shepherd"></constructor-arg> <constructor-arg value="twenty"></constructor-arg> </bean>
Output of the Scenario 3 :
Scenario 1 and Scenario 3, have only one change instead of providing value as 20 we have provided "twenty" a String value. Spring cannot convert "twenty" to int and throws java.lang.NumberFormatException. Think of it as Spring cannot perform something like Integer.parseInt("String") .


