Donate : Link
Medium Blog : Link
Applications : Link
Spring Tutorial Index Page: Link
- In parameterized constructor we can pass value using below code.
Test(String name) {
this.name = name
}
<bean id="t" class="com.Test">
<constructor-arg value = "Code" />
OR
<constructor-arg>
<value>Code</value>
</constructor-arg>
</bean>
- In Test class we have only one constructor with String parameter. Now if i declare another constructor with integer parameter then what?
- Then we must have to define type attribute. If we not define it then it will call constructor with String parameter.
Test(int age) {
this.age = age;
}
<bean id="t" class="com.Test">
<constructor-arg value = "Code" type="int"/>
OR
<constructor-arg>
<value>123</value>
</constructor-arg>
</bean>
- If we have constructor with 2 parameters String and Integer. If we are providing inputs like below code (w/o type attribute) then it will throw type casting exception because it will try to bind “123” value to String and “Code” value to age. To solve this we must have to use type attribute.
Test(String name, int age) {
this.name = name;
this.age = age;
}
<bean id="t" class="com.Test">
<constructor-arg value = "123" type="int"/>
<constructor-arg value = "Code" type="string"/>
</bean>
- Lets we add one more attribute in Test class and constructor. If we define values based on below code then it will get ambiguity here.
Test(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
<bean id="t" class="com.Test">
<constructor-arg value = "123" type="int"/>
<constructor-arg value = "Code" type="string"/>
<constructor-arg value = "code@factory.com" type="string"/>
</bean>
- We can solve above issue using providing the values by ordering. If we pass “Code”, “123”, and “code@factory.com” then it will not get ambiguity here. If we don’t want to follow order then we can provide value by using index. Index start from 0. If we use index again then it will pass latest value. In below example it will use “code@code.com” for index 2.
<bean id="t" class="com.Test">
<constructor-arg value = "123" index="1"/>
<constructor-arg value = "Code" index="0"/>
<constructor-arg value = "code@factory.com" index="2"/>
<constructor-arg value = "code@code.com" index="2"/>
</bean>
- In setter based we have name attribute but in constructor based we don’t have name attribute. We have to use type or index.
- For primitive type values we use value attribute but for secondary we use ref (reference) attribute.
Create Java Project
- Open Eclipse
- Go to File -> New -> Others… -> Java Project
- Create DI-Constructor project
- Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs
- commons-logging-X.X.jar
- spring-beans-X.X.X.jar
- spring-context-X.X.X.jar
- spring-core-X.X.X.jar
- spring-expression-X.X.X.jar
- * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)

spring.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="t" class="com.codeFactory.beans.Test">
<!-- pass values sequentially -->
<!-- <constructor-arg value="Code" />
<constructor-arg value="20" />
<constructor-arg value="Male" /> -->
<!-- use type -->
<!-- <constructor-arg value="20" type="int" />
<constructor-arg value="Code" type="java.lang.String" />
<constructor-arg value="Male" type="java.lang.String" /> -->
<!-- use index -->
<constructor-arg value="20" index="1" />
<constructor-arg value="Code" index="0" />
<constructor-arg value="Male" index="2" />
</bean>
</beans>
Test.java
package com.codeFactory.beans;
/**
* @author code.factory
*
*/
public class Test {
String name;
int age;
String gender;
Test(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void print() {
System.out.println("Name " + name);
System.out.println("Age " + age);
System.out.println("Gender " + gender);
}
}
Client.java
package com.codeFactory.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.codeFactory.beans.Test;
/**
* @author code.factory
*
*/
public class Client {
public static void main(String... args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
Test t = (Test) context.getBean("t");
t.print();
}
}
Output:
Dec 11, 2020 9:29:06 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4bf558aa: startup date [Fri Dec 11 21:29:06 IST 2020]; root of context hierarchy
Dec 11, 2020 9:29:06 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/codeFactory/resources/spring.xml]
Name Code
Age 20
Gender Male

