Additional Configurations In Spring Boot Application

Additional Config

Here I am going to tell about additional config or configurations in your spring boot application. Now what could be the additional configurations in your spring boot application. Generally you define configurations into application.properties file under class path folder.

But what if your application needs more configurations which could be supplied during runtime or the jar which is produced from this spring boot application can be used into other application and the other application already provides those additional configurations. So, you don’t need to define these additional configurations into the application.properties file.

In this situation you may provide another properties file location with the spring’s standard variable SPRING_CONFIG_ADDITIONAL_LOCATION and all key/value pairs will be picked up by the spring boot application.

For example, in Eclipse IDE you can specify the additional properties file in the Run Configurations window under Environment tab. This Run Configurations window you can find by doing right click on the main class.

spring additional config

Prerequisites

Java 19, Maven 3.8.5, Spring Boot 3.0.6/3.1.0

Build File

The following pom.xml build file can be used as a reference for the example.

<?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-boot-additional-config</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>19</maven.compiler.source>
		<maven.compiler.target>19</maven.compiler.target>
	</properties>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.1.0</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 Config

The following src/main/resources/application.properties file contains the regular application configuration key/value pair(s).

server.port=8080

Additional Config

The additional configuration can be defined in a properties file which is not available in the spring boot application’s class path folder. The following content is kept into a file called additional-properties.properties file and you can place it anywhere in the system.

hi=Hi
hello=Hello

Inject Config

You need to inject the same way you inject key from application.properties file. For example, let look at the following main class where I have injected all keys from regular and additional config files.

@SpringBootApplication
public class App implements CommandLineRunner {
	
	@Value("${server.port}")
	private int port;

	@Value("${hi}")
	private String hi;

	@Value("${hello}")
	private String hello;

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

	@Override
	public void run(String... args) throws Exception {
		System.out.println("server.port => " + port);
		System.out.println("hi => " + hi + ", hello => " + hello);
	}

}

Make sure you specify the location of the additional config file using the standard spring variable name.

Testing Additional Config

If you do not specify the additional config file then you will see the following exception:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'hi' in value "${hi}"

If you specify the additional config file then you will see the following output:

server.port => 8080
hi => Hi, hello => Hello

Source Code

Download

Leave a Reply

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