Setter Dependency Injection with Array of Object / Non string
In some scenarios, we may need to inject an array of custom object to another object.
Consider the example where Person has Addresses
In this case, we can use Array of Address which stores the addresses
Let us see the same in spring through Setter Dependency Injection
Create a class called Person
It contains 3 attributes id,name and addresses where addresses is an Array of Address.
It contains 3 setter methods one for each attribute
It also contains showAddresses() method to display the addresses injected to the person object.
- package com.kb.di;
- public class Person {
- private int id;
- private String name;
- private Address[] addresses;
- public void showAddresses() {
- int index = 1;
- for (Address address : addresses) {
- System.out.println("Address " + index + ":");
- System.out.println(address);
- index++;
- }
- }
- public void setId(int id) {
- this.id = id;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setAddresses(Address[] addresses) {
- this.addresses = addresses;
- }
- }
package com.kb.di; public class Person { private int id; private String name; private Address[] addresses; public void showAddresses() { int index = 1; for (Address address : addresses) { System.out.println("Address " + index + ":"); System.out.println(address); index++; } } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setAddresses(Address[] addresses) { this.addresses = addresses; } }
create the Address class
- package com.kb.di;
- public class Address {
- private int houseNo;
- private String streetName;
- private String city;
- private String postalCode;
- @Override
- public String toString() {
- return "House No-> " + houseNo + " Street Name-> " + streetName + " City->" + city + " Postal Code->"
- + postalCode;
- }
- public void setHouseNo(int houseNo) {
- this.houseNo = houseNo;
- }
- public void setStreetName(String streetName) {
- this.streetName = streetName;
- }
- public void setCity(String city) {
- this.city = city;
- }
- public void setPostalCode(String postalCode) {
- this.postalCode = postalCode;
- }
- }
package com.kb.di; public class Address { private int houseNo; private String streetName; private String city; private String postalCode; @Override public String toString() { return "House No-> " + houseNo + " Street Name-> " + streetName + " City->" + city + " Postal Code->" + postalCode; } public void setHouseNo(int houseNo) { this.houseNo = houseNo; } public void setStreetName(String streetName) { this.streetName = streetName; } public void setCity(String city) { this.city = city; } public void setPostalCode(String postalCode) { this.postalCode = postalCode; } }
Create a spring bean definition file
In this file, we used < property > tag with < array > to define the addresses
< array > tag is used to inject the addresses defined as array of Address in the Person class.
- <?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.2.xsd">
- <bean id="person" class="com.kb.di.Person">
- <property name="id" value="1"></property>
- <property name="name" value="Raj"></property>
- <property name="addresses">
- <array>
- <ref bean="add1" />
- <ref bean="add2" />
- </array>
- </property>
- </bean>
- <bean id="add1" class="com.kb.di.Address">
- <property name="houseNo" value="33" />
- <property name="streetName" value="M G Road" />
- <property name="city" value="Bangalore" />
- <property name="postalCode" value="560001" />
- </bean>
- <bean id="add2" class="com.kb.di.Address">
- <property name="houseNo" value="44" />
- <property name="streetName" value="Residency Road" />
- <property name="city" value="Mumbai" />
- <property name="postalCode" value="400051" />
- </bean>
- </beans>
<?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.2.xsd"> <bean id="person" class="com.kb.di.Person"> <property name="id" value="1"></property> <property name="name" value="Raj"></property> <property name="addresses"> <array> <ref bean="add1" /> <ref bean="add2" /> </array> </property> </bean> <bean id="add1" class="com.kb.di.Address"> <property name="houseNo" value="33" /> <property name="streetName" value="M G Road" /> <property name="city" value="Bangalore" /> <property name="postalCode" value="560001" /> </bean> <bean id="add2" class="com.kb.di.Address"> <property name="houseNo" value="44" /> <property name="streetName" value="Residency Road" /> <property name="city" value="Mumbai" /> <property name="postalCode" value="400051" /> </bean> </beans>
Create a pom.xml file
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>Spring</groupId>
- <artifactId>SpringCore</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>SpringCore</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>4.2.4.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>4.2.4.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Spring</groupId> <artifactId>SpringCore</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringCore</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
Create a SetterDI class
This class loads the spring beans using application context and calls the person class method to display the addresses.
- package com.kb.di;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class SetterDI {
- public static void main(String[] args) {
- ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
- Person person = (Person)applicationContext.getBean("person");
- person.showAddresses();
- }
- }
package com.kb.di; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SetterDI { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml"); Person person = (Person)applicationContext.getBean("person"); person.showAddresses(); } }
Run the SetterDI class and see the output as below