Spring Constructor Injection With Dependent Object

Spring Constructor Injection With Dependent 

HELLO READERS, in this spring tutorial we are going to learn about spring constructor injection with dependent object. In this example we are going to learn how to inject value to a dependent object through constructor injection.

Consider the below example Players bean class which contains fullName, role, board attributes and address (Note. Here Address is dependent object for Players).

Here, our scenario is Players HAS-A Address.

The toString() method(for textual representation of object).

package com.TheTechMatin.Entity;

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

public Address() {}

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

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


package com.TheTechMatin.Entity;

public class Players {

     private String fullName;
     private String role;
     private String board;
     private Address address;

     public Players() {


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

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

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

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

     <bean id="address" class="com.TheTechMatin.Entity.Address">
           <constructor-arg value="Jam Nagar"></constructor-arg>
           <constructor-arg value="Gujrat"></constructor-arg>
           <constructor-arg value="India"></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="address"/>
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 ref attribute which is the sub-attribute of <constructor-arg> is used to define a reference of another object, in this way we are passing the dependent object as an constructor argument.

Now executing the TestApp.java to test the application.
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

//close the context

-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, Address [city=Jam Nagar, state=Gujrat, country=India]

-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