Spring Constructor Injection Using List With Dependent Object


Spring Constructor Injection Using List With Dependent Object
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<Address> to store Players multiple address, similarly what we have done in previous tutorial for List<String> but here object is user defined i.e Address class.

Info  :  List can contain duplicate object.

Here Address class contains property such as city and country with constructors and also having toString() method.

package com.TheTechMatin.Entity;

public class Address {
private String city;
private String country;

public Address() {}

public Address(String city, String country) {
     this.city = city;
     this.country = country;

public String toString() {
     return "Address [city=" + city + ",country=" + country+"]";


Here players class contains fullName, role, board and List<Address> with constructor and with displayInfo() method which is responsible to display Players information in convenient way.

package com.TheTechMatin.Entity;

import java.util.List;

public class Players {

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


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

     public void displayInfo() {
          System.out.println("Name :"+fullName +" Role :"+role+" Board :"+board);
          int count = 0;
          for(Address ad : playersAddress) {
              System.out.println("Addresss "+count+" :"+ad);

This applicationContext.xml file is a configuration file in which we have injected the values to a particular fields via constructor injection using <constructor-arg> element and using the things which we have already seen in our last tutorials.

<?xml version="1.0" encoding="UTF-8"?>

     <bean id="addressOfIndia" class="com.TheTechMatin.Entity.Address">
      <constructor-arg value="Jam Nagar"></constructor-arg>
      <constructor-arg value="India"></constructor-arg>
     <bean id="addressOfEngland" class="com.TheTechMatin.Entity.Address">
       <constructor-arg value="London"></constructor-arg>
      <constructor-arg value="England"></constructor-arg>
     <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>
            <ref bean="addressOfIndia"/>
            <ref bean="addressOfEngland"/>

In TestApp.java class first we are loading applicationContext.xml file which is responsible for configurations, then printing a message after that getting bean form config file and then calling displayInfo() method and at the lass closing the context

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);
          //call method to display info
          //close the context



On execution of main class the following output will be displayed.

Hello Welcome To Sping Constructor Injection!
Name :Sir Ravindra Jadeja Role :All Rounder Board :BCCI
Addresss 1 :Address [city=Jam Nagar,country=India]
Addresss 2 :Address [city=London,country=England]
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