How to Configure POJOs with Java Collection Attributes

LinkedIn
Hacker News

Java POJO can contain simple primitive data types or collections. Like the primitive types, the Spring IoC container can instantiate POJOs with Java collection attributes. Java provides different types of collection types including set, list, map, etc. All of the collection types has their specific characteristics.

In Spring all these collection types can be configured using set of built-in XML tags such as <list>, <set>, and <map>. In this example, we will discuss how to configure the the List collection. However the same concept will apply to other collection types.

Let us first, declare the following POJOs.

-Advertisement-

User.java
public class User {
	private String name;
	private String email;
	private List<Car> cars;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public List<Car> getCars() {
		return cars;
	}

	public void setCars(List<Car> cars) {
		this.cars = cars;
	}

	@Override
	public String toString() {
		return "Name=" + name + " , Email=" + email + " , Car=" + cars.toString();
	}
}
Car.java
public class Car {
	private String model;
	private double price;

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return model + " " + price;
	}
}

As you can notice, a user can own a list of Cars. Let us now configure the bean inside beans.xml file.

To define a property of the interface java.util.List in the bean configuration, you specify a <list> tag that contains the elements. The elements allowed inside the <list> tag can be a simple constant value specified by <value>.

beans.xml
<bean id="userBean" class="com.javatechig.spring.collection.User">
	<property name="name" value="Nilanchala" />
	<property name="email" value="test@example.com" />
	<property name="cars">
		<list>
			<bean class="com.javatechig.spring.collection.Car">
				<property name="model" value="Volkswagen Polo" />
				<property name="price" value="5000" />
			</bean>

			<bean class="com.javatechig.spring.collection.Car">
				<property name="model" value="Volkswagen Vento" />
				<property name="price" value="9000" />
			</bean>
			<null />
		</list>
	</property>
</bean>

Now from the main class you can test the above configuration as follows.

Main.java
public class Main {
	public static void main(String[] args) throws Exception {
		ApplicationContext context = new GenericXmlApplicationContext("beans.xml");
		User user = (User) context.getBean("userBean");
		System.out.println(user.toString());
	}
}

If you want to configure the list by refering to other beans, you can do it using <ref> element.

<bean id="userBean" class="com.javatechig.spring.collection.User">
	<property name="name" value="Nilanchala" />
	<property name="email" value="test@example.com" />
	<property name="cars">
		<list>
			<ref bean="myCarBean1"/>
			<ref bean="myCarBean2"/>
		</list>
	</property>
</bean> 

Tagged in : Spring Core

Related Articles

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.