6 minutes

Configure POJOs with Java Collection Attributes in Spring

By Nilanchala @nilan, On Sep 17, 2023 Spring 2.52K Views

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 have their specific characteristics.

In Spring all these collection types can be configured using a set of built-in XML tags such as , , and

. In this example, we will discuss how to configure the List collection. However, the same concept will apply to other collection types.

Let us first declare the following POJOs.

public class User {
    private String name;
    private String email;
    private List cars;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getEmail() {
        return email;

    public void setEmail(String email) { = email;

    public List getCars() {
        return cars;

    public void setCars(List cars) { = cars;

    public String toString() {
        return "Name=" + name + " , Email=" + email + " , Car=" + cars.toString();

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;

    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 tag that contains the elements. The elements allowed inside the tag can be a simple constant value specified by .


Now from the main class you can test the above configuration as follows.
public class Main {
    public static void main(String[] args) throws Exception {
        ApplicationContext context = new GenericXmlApplicationContext("beans.xml");
        User user = (User) context.getBean("userBean");

If you want to configure the list by referring to other beans, you can do it using element.

nilan avtar


I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.