SPRING CONSTRUCTOR INJECTION EXAMPLE WITH XML CONFIG

Share:
HELLO READERS, In this spring tutorial we are going to learn about spring constructor injection. In this example we are going to learn how to set value to a bean property through constructor injection.

Consider the below example Players  bean class which contains fullName, role and board attributes with default and parameterized constructor and toString() method(for textual representation of object).

Players.java
package com.TheTechMatin.Entity;

public class Players {

     private String fullName;
     private String role;
     private String board;

     public Players() {
     }

     public Players(String fullName, String role, String board) {
           this.fullName = fullName;
           this.role = role;
           this.board = board;
     }

     @Override
     public String toString() {
           return "Players [fullName=" + fullName + ", role=" + role + ", board=" + board + "]";
     }
}

The Players class has three attributes i.e., name, role and board and a default constructor and a parameterized constructor (basic convention) . The toString() method is used to represent textual form of the Players object.

The applicationContext.xml fie is used to do the spring bean configuration. The below xml file shows how to set/assign a value to bean property through spring constructor injection.

 applicationContext.java
<?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="player" class="com.TheTechMatin.Entity.Players">
           <constructor-arg value="Sir Ravindra Jadeja" type="String"></constructor-arg>
           <constructor-arg value="All Rounder"></constructor-arg>
           <constructor-arg value="BCCI"></constructor-arg>
     </bean>
</beans>

The id attribute of the bean element is used to specify the bean name and class attribute is used to specify the fully qualified class name of the bean.

The <constructor-arg> element which is the sub-element of <bean> will invokes the constructor with matching number of argument and will inject the value of value attribute to a particular attributes. 

The type attribute is optional for String type of attribute as by default it will be injected to string type of attributes of bean class, but in case of other data type u have to specify the type of attribute for example for id of int type the syntax would be <constructor-arg value="10" type="int"></constructor-arg> then the matching argument constructor will be invoked.

Here in this example we set “Sir Ravindra Jadeja” for fullName attribute of Players class similarly “All Rounder” for role attribute and “BCCI” for board attribute thru constructor injection.

Now executing the TestApp.java to test the application.
TestApp.java
package com.TheTechMatin.Entity;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApp {

     public static void main(String[] args) {

//load configuration file
           ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

//just printing msg
           System.out.println("Hello Welcome To Sping Constructor Injection!");

//get the bean from applicationContext.xml
           Players player = context.getBean(Players.class);

//print the Players attributes value
           System.out.println(player);

//close the context
           context.close();
     }
}

So when we execute the main class the following output will be displayed on the console.

Hello Welcome To Sping Constructor Injection!
Players [fullName=Sir Ravindra Jadeja, role=All Rounder, board=BCCI]
Tested On Eclipse IDE 2019-06.

No comments

You May Like Also

Programming Knowledge


Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database