Spring Constructor Injection With Dependent
Object
Object
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).
Address.java
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;
}
@Override
public String toString() {
return "Address
[city=" + city + ", state=" + state + ", country=" + country;
}
}
Players.java
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;
}
@Override
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.
applicationContext.xml
<?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="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>
<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>
<constructor-arg>
<ref bean="address"/>
</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 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.
TestApp.java
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, Address [city=Jam
Nagar, state=Gujrat, country=India]
-Tested On Eclipse IDE 2019-06.
No comments