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.
Address.java
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;
}
@Override
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.
Players.class
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) {
count++;
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.
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="addressOfIndia" class="com.TheTechMatin.Entity.Address">
<constructor-arg value="Jam
Nagar"></constructor-arg>
<constructor-arg value="India"></constructor-arg>
</bean>
<bean id="addressOfEngland" class="com.TheTechMatin.Entity.Address">
<constructor-arg value="London"></constructor-arg>
<constructor-arg value="England"></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>
<list>
<ref bean="addressOfIndia"/>
<ref bean="addressOfEngland"/>
</list>
</constructor-arg>
</bean>
</beans>
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
TestApp.java
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
player.displayInfo();
//close the
context
context.close();
}
}
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