English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Example of dependency constructor injection

We can inject dependencies through the constructor. <bean> <constructor-arg>Sub-elements are used for constructor injection. Here, we need to inject

Original and string-based values Subordinate objects (including objects) Collection values, etc.

Injection of original values and string-based values

Let's take a look at simple examples of injecting original values and string-based values. We have created three files here:

Employee.java applicationContext.xml Test.java

Employee.java

This is a simple class that contains two fields id and name. This class has four constructors and one method.

package com.w;3codebox;
public class Employee {
private int id;
private String name;
public Employee() { System.out.println("def cons"); }
public Employee(int id) { this.id = id; }
public Employee(String name) { this.name = name; }
public Employee(int id, String name) {
    this.id = id;
    this.name = name;
}
void show() {
    System.out.println(id+" "+name);
}
}


applicationContext.xml

We provide information to the Bean through this file. constructor-The arg element calls the constructor. In this case, the parameterized constructor of the int type will be called. Constructor-The value attribute of the arg element assigns the specified value. The type attribute specifies that the int parameter constructor will be called.

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
    xmlns:p="http://www.springframework.org/schema/p
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="e" class="com.w3codebox.Employee">
<constructor-arg value="10" type="int"></constructor-arg>
</bean>
</beans>

Test.java

This class retrieves Beans from applicationContext.xml file and calls the show method.

package com.w;3codebox;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;
public class Test {
    public static void main(String[] args) {
        
        Resource r = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(r);
        
        Employee s = (Employee)factory.getBean("e");
        s.show();
        
    }
}

Output: 10Empty


Injection of string-based values

If you do not specify the type attribute in the constructor arg element, the string type constructor will be called by default.

....
<bean id="e" class="com.w3codebox.Employee">
<constructor-arg value="10></constructor-arg>
</bean>
....

If the bean element is changed as described above, the string parameter constructor will be called, and the output will be 0 10.

Output: 0 10


You can also pass string literals as shown below:

....
<bean id="e" class="com.w3codebox.Employee">
<constructor-arg value="Sonoo"></constructor-arg>
</bean>
....

Output: 0 Sonoo


You can pass integer literals and string literals in the following ways

....
<bean id="e" class="com.w3codebox.Employee">
<constructor-arg value="10" type="int"></constructor-arg>
<constructor-arg value="Sonoo"></constructor-arg>
</bean>
....

Output: 10 Sonoo