How to inject array in Spring


In this Spring tutorial, we will learn how to inject multiple value or Array to the bean. String array or integer array can hold multiple values. To inject multiple values, we have <list /> subelement of <property /> and <constructor-arg /> element.

For setter-based dependency injection, these configuration works fine.

<property name="carNames">
    <list>
        <value>Hindustan Moters</value>
        <value>Tata Moters</value>
        <value>Ashoka Leyland</value>
    </list>
</property>

And for constructor-based dependency injection, configuration looks like…

<constructor-arg index="0">
    <list>
        <value>Hindustan Moters</value>
        <value>Tata Moters</value>
        <value>Ashoka Leyland</value>
    </list>
</constructor-arg>

Or if you separate your values through a comma, Spring will do the conversion for you where values is a String[] array.

<property name="carNames" value="Hindustan Moters,Tata Moters,Ashoka Leyland"/>

Note: You can also use the <array/> subelement but must go with <list/> for future perspective.

Check the complete example.

Spring Beans

Create a class Engine that have a primitive string array field.

Engine.java
package org.websparrow.beans;

public class Engine {

	// generate getters and setters...
	private String modelYear; // primitive string

	public String getModelYear() {
		return modelYear;
	}

	public void setModelYear(String modelYear) {
		this.modelYear = modelYear;
	}
}

Create another class Car that has one primitive string array and one secondary string array fields. And the business logic that actually uses the injected values.

Car.java
package org.websparrow.beans;

public class Car {

	// generate setters...
	private String[] carNames; // primitive string array
	private Engine[] engines; // secondary string array

	public void setCarNames(String[] carNames) {
		this.carNames = carNames;
	}

	public void setEngines(Engine[] engines) {
		this.engines = engines;
	}

	// business logic
	public void carDetails() {

		System.out.println("CAR NAME......");
		for (String car : carNames) {
			System.out.println(car);
		}
		System.out.println("\nMODEL YEAR......");
		for (Engine engine : engines) {
			System.out.println(engine.getModelYear());
		}
	}
}

Spring Bean Configuration

Inside the configuration file, create three different bean of Engine class and pass the bean id to Car class for secondary type array using <ref bean="e1" /> subelement of the list.

spring.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="c" class="org.websparrow.beans.Car">
		<property name="carNames">
			<list>
				<value>Hindustan Moters</value>
				<value>Tata Moters</value>
				<value>Ashoka Leyland</value>
			</list>
		</property>
		<property name="engines">
			<list>
				<ref bean="e1" />
				<ref bean="e2" />
				<ref bean="e3" />
			</list>
		</property>
	</bean>

	<bean id="e1" class="org.websparrow.beans.Engine">
		<property name="modelYear" value="1991" />
	</bean>
	<bean id="e2" class="org.websparrow.beans.Engine">
		<property name="modelYear" value="1992" />
	</bean>
	<bean id="e3" class="org.websparrow.beans.Engine">
		<property name="modelYear" value="1993" />
	</bean>

</beans>

Run it

Load the configuration and run it by calling getBean() method. Pass the Car class bean id in getBean() method.

Dealer.java
package org.websparrow.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.websparrow.beans.Car;

public class Dealer {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

		Car car = (Car) context.getBean("c");
		car.carDetails();
	}
}
Output:

You can see the following result on console log.

CAR NAME......
Hindustan Moters
Tata Moters
Ashoka Leyland

MODEL YEAR......
1991
1992
1993

Download Source Code: how-to-inject-array-in-spring.zip


Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.