Working with Collection Element Props in Spring Application

With this example I will show you how to inject Collections type Props in Spring applications. This is similar to the element type Map I had shown in another example.

I am going to use Spring Boot framework but I will show you both using application.properties file as well as traditional XML config file to configure the props elements.

In order to show how Collections type Props can be injected in a Spring Bean I will create a simple Spring Bean with a property Properties.

Prerequisites

Java at least 8, Spring Boot 2.4.1, Maven 3.6.3, Gradle 6.7.1

Project Setup

You can create a gradle or maven based project in your favorite IDE or tool.

For gradle based project you can use the following build.gradle script:

buildscript {
	ext {
		springBootVersion = '2.4.1'
	}
	
    repositories {
    	maven {
    		url 'https://plugins.gradle.org/m2/'
    	}
    }
    
    dependencies {
    	classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
	implementation("org.springframework.boot:spring-boot-starter:${springBootVersion}")
}

For the maven based project you can use the following pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<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>com.roytuts</groupId>
	<artifactId>spring-collection-props</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>12</maven.compiler.source>
		<maven.compiler.target>12</maven.compiler.target>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.1</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

Application Properties

Create src/main/resources folder if it does not exist already. Under this folder you need to create an application.properties file for props configuration.

Injecting Collection – Props

You define key/value pair at each line in the application.properties file and you inject these key/value pairs with the @ConfigurableProperties annotation or using Environment interface or using the @Value annotation but these are not formed as elements of props data structure.

Here I am going to show you how you can configure as props elements in the application.properties file. This is similar to the map elements.

props.elements={site:'https://roytuts.com',author:'Soumitra',about:'https://roytuts.com/about',facebook:'https://www.facebook.com/roytuts2014',twitter:'https://twitter.com/roytuts',youtube:'https://www.youtube.com/channel/UCMysotNzptWegWBSY9lox_g',ello:'https://ello.co/soumitrajuster',linkedin:'https://www.linkedin.com/in/soumitra-sarkar-80ba0328/'}

In Java configuration you need to pick these values in the following way:

@Value("#{${props.elements}}")
private Properties props;

The whole source code can be downloaded from the Source Code section later in this tutorial.

Another way is to use the traditional way of using the XML config to define a props with key/value pairs and read this map in the 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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean class="com.roytuts.spring.collection.props.RoytutsInfoProps">
		<property name="props">
			<props>
				<prop key="site">https://roytuts.com</prop>
				<prop key="author">Soumitra</prop>
				<prop key="about">https://roytuts.com/about</prop>
				<prop key="facebook">https://www.facebook.com/roytuts2014</prop>
				<prop key="twitter">https://twitter.com/roytuts</prop>
				<prop key="youtube">https://www.youtube.com/channel/UCMysotNzptWegWBSY9lox_g
				</prop>
				<prop key="ello">https://ello.co/soumitrajuster</prop>
				<prop key="linkedin">https://www.linkedin.com/in/soumitra-sarkar-80ba0328/
				</prop>
			</props>
		</property>
	</bean>
</beans>

The corresponding Java class for the above bean is given below:

package com.roytuts.spring.collection.props;

import java.util.Properties;

public class RoytutsInfoProps {

	private Properties props;

	public Properties getProps() {
		return props;
	}

	public void setProps(Properties props) {
		this.props = props;
	}

}

Finally you can use the below configuration to load the XML based bean configuration in Spring Boot application:

@Configuration
@ImportResource("classpath:props.xml")
public class Config {

}

Main Class

A class with main method and @SpringBootApplication annotation is enough to start the Spring Boot application. I am using CLI version for starting the app.

package com.roytuts.spring.collection.props;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringCollectionPropsApp implements CommandLineRunner {

	@Autowired
	private SpringCollectionProps collectionMap;

	@Autowired
	private ApplicationContext applicationContext;

	public static void main(String[] args) {
		SpringApplication.run(SpringCollectionPropsApp.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		collectionMap.getProps().forEach((k, v) -> System.out.println(k + " -> " + v));

		System.out.println("========================================================");

		RoytutsInfoProps roytutsInfoMap = applicationContext.getBean(RoytutsInfoProps.class);

		roytutsInfoMap.getProps().forEach((k, v) -> System.out.println(k + " -> " + v));
	}

}

Testing the Application

Running the above main class you will see the following output:

youtube -> https://www.youtube.com/channel/UCMysotNzptWegWBSY9lox_g
site -> https://roytuts.com
twitter -> https://twitter.com/roytuts
ello -> https://ello.co/soumitrajuster
author -> Soumitra
facebook -> https://www.facebook.com/roytuts2014
about -> https://roytuts.com/about
linkedin -> https://www.linkedin.com/in/soumitra-sarkar-80ba0328/
========================================================
youtube -> https://www.youtube.com/channel/UCMysotNzptWegWBSY9lox_g
site -> https://roytuts.com
twitter -> https://twitter.com/roytuts
ello -> https://ello.co/soumitrajuster
author -> Soumitra
facebook -> https://www.facebook.com/roytuts2014
about -> https://roytuts.com/about
linkedin -> https://www.linkedin.com/in/soumitra-sarkar-80ba0328/

That’s all about how to work with collection type props in Spring applications.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *