Spring Constructor Injection With List

Spring Constructor Injection With List

In spring we can inject values to Collection by constructor injection. We can use three elements in <constructor-arg> element,  it can be list, set and map etc.

In this example we are going to use list to store Players batting style, bowling style and nick name.

Info  :  list can contain duplicate elements.

package com.TheTechMatin.Entity;
import java.util.List;

public class Players {

     private String fullName;
     private String role;
     private String board;
     private List<String> playersInfo;
     public Players() {


     public Players(String fullName, String role, String board, List<String> playersInfo) {
          this.fullName = fullName;
          this.role = role;
          this.board = board;
          this.playersInfo = playersInfo;

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

The <list> element of <constructor-arg> is used here to define the list. 
<?xml version="1.0" encoding="UTF-8"?>

     <bean id="player" class="com.TheTechMatin.Entity.Players">
          <constructor-arg value="Sir Ravindra Jadeja"></constructor-arg>
          <constructor-arg value="All Rounder"></constructor-arg>
          <constructor-arg value="BCCI"></constructor-arg>
           <!-- batting style -->
            <value>Left Hand Batting</value>
            <!-- bowling style -->
            <value>Left Hand Bowling</value>
            <!-- nick name -->

This is the main class in which first loading config file then getting bean from it.
package com.TheTechMatin.Entity;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApp {

     public static void main(String[] args) {
          //load the configuration file
          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
          //just printing the message
          System.out.println("Hello Welcome To Sping Constructor Injection!");
          //get the bean
          Players player = context.getBean(Players.class);
          //print the Players class
          //close the context

On execution of TestApp.java class the following output will be displayed on console.

Hello Welcome To Sping Constructor Injection!
Players [fullName=Sir Ravindra Jadeja, role=All Rounder, board=BCCI, playersInfo=[Left Hand Batting, Left Hand Bowling, Jaddu]]
Tested On Eclipse 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