W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/10/2017

Spring Constructor Injection-Spring Tutorial

We can inject the dependency by constructor. The <constructor-arg> subelement of <bean> is used for constructor injection. Here we are going to inject
  1. primitive and String-based values
  2. Dependent object (contained object)
  3. Collection values etc.

Injecting primitive and string-based values

Let's see the simple example to inject primitive and string-based values. We have created three files here:

  • Employee.java
  • applicationContext.xml
  • Test.java
Employee.java
It is a simple class containing two fields id and name. There are four constructors and one method in this class.
  1. package com.javatpoint;  
  2.   
  3. public class Employee {  
  4. private int id;  
  5. private String name;  
  6.   
  7. public Employee() {System.out.println("def cons");}  
  8.   
  9. public Employee(int id) {this.id = id;}  
  10.   
  11. public Employee(String name) {  this.name = name;}  
  12.   
  13. public Employee(int id, String name) {  
  14.     this.id = id;  
  15.     this.name = name;  
  16. }  
  17.   
  18. void show(){  
  19.     System.out.println(id+" "+name);  
  20. }  
  21.   
  22. }  
applicationContext.xml
We are providing the information into the bean by this file. The constructor-arg element invokes the constructor. In such case, parameterized constructor of int type will be invoked. The value attribute of constructor-arg element will assign the specified value. The type attribute specifies that int parameter constructor will be invoked.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  8.   
  9. <bean id="e" class="com.javatpoint.Employee">  
  10. <constructor-arg value="10" type="int"></constructor-arg>  
  11. </bean>  
  12.   
  13. </beans>  
Test.java
This class gets the bean from the applicationContext.xml file and calls the show method.
  1. package com.javatpoint;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  5. import org.springframework.core.io.*;  
  6.   
  7. public class Test {  
  8.     public static void main(String[] args) {  
  9.           
  10.         Resource r=new ClassPathResource("applicationContext.xml");  
  11.         BeanFactory factory=new XmlBeanFactory(r);  
  12.           
  13.         Employee s=(Employee)factory.getBean("e");  
  14.         s.show();  
  15.           
  16.     }  
  17. }  
Output:10 null

Injecting string-based values

If you don't specify the type attribute in the constructor-arg element, by default string type constructor will be invoked.
  1. ....  
  2. <bean id="e" class="com.javatpoint.Employee">  
  3. <constructor-arg value="10"></constructor-arg>  
  4. </bean>  
  5. ....  
If you change the bean element as given above, string parameter constructor will be invoked and the output will be 0 10.
Output:0 10

You may also pass the string literal as following:
  1. ....  
  2. <bean id="e" class="com.javatpoint.Employee">  
  3. <constructor-arg value="Sonoo"></constructor-arg>  
  4. </bean>  
  5. ....  
Output:0 Sonoo

You may pass integer literal and string both as following
  1. ....  
  2. <bean id="e" class="com.javatpoint.Employee">  
  3. <constructor-arg value="10" type="int" ></constructor-arg>  
  4. <constructor-arg value="Sonoo"></constructor-arg>  
  5. </bean>  
  6. ....  
Output:10 Sonoo

No comments:

Post a Comment

Note: only a member of this blog may post a comment.