Spring Boot application.properties Inheritance/Override

Table of Contents

Introduction

In this example I am going to show you how to inherit values from application.properties into different environments. So, if you have an application.properties file under class path folder src/main/resources and if you have different environment specific application-{env}.properties files, then you csan inherit key/value pairs from application.properties file. Even you can override the inherited key/value pair from application.properties into environment specific application-{env}.properties file.

Prerequisites

Java 1.8+, Maven 3.8.2, Spring Boot 2.6.2

Project Setup

You can use the following pom.xml file for your maven based project:

<?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-application-properties-inheritance</artifactId>
	<version>0.0.1-SNAPSHOT</version>

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

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

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

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

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

</project>

Inheritance/Override

Now I will show you how to override the key/value pair from application.properties file into environment specific application-{env}.properties file. The application or application-{env} properties files are kept under src/main/resources folder.

So, let’s say you have a key/value pair app.key=app key in application.properties file and I have another file application-local.properties with a key/value pair as app.key=app local key.

When I run the Spring Boot application without profile, then it will show app key as an output. If I run the application with local profile then it will show the value as app local key.

application.properties

app.key=app key

application-local.properties

app.key=app local key

Main Class

The following class with main method has been created to test the application.

@SpringBootApplication
public class Application implements CommandLineRunner {

	@Value("${app.key}")
	private String appKey;

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

	@Override
	public void run(String... args) throws Exception {
		System.out.println("appKey: " + appKey);
	}

}

Testing the Application

Run the above main class without profile (default profile), the following output is produced:

appKey: app key

When you run the main class with local profile (by passing -Dspring.profiles.active=local as a VM argument), then your application produces the following output:

appKey: app local key

So, the value got overriden by the local profile in the Spring Boot application.

Inherit from application.properties

Now let’s say I am adding another key/value pair to the application.properties file:

app.inherit=inherited value

So, the application.properties file has the following content:

app.key=app key
app.inherit=inherited value

I have not updated the application-local.properties file.

Now update the main class to print the value:

@SpringBootApplication
public class Application implements CommandLineRunner {

	@Value("${app.key}")
	private String appKey;

	@Value("${app.inherit}")
	private String appInherit;

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

	@Override
	public void run(String... args) throws Exception {
		System.out.println("appKey: " + appKey);
		System.out.println("appInherit: " + appInherit);
	}

}

Now when you run the main class with local profile, you will see the following output:

appKey: app key
appInherit: inherited value

So, the value from application.properties file has been inherited into local profile though I have not declared it into application-local.properties file.

Source Code

Download

Leave a Reply

Your email address will not be published.