Spring Boot and multiple external configuration files

Cover Image for Spring Boot and multiple external configuration files
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Spring Boot and Multiple External Configuration Files: Defying Defaults 💥

Are you struggling with multiple property files and configuration overrides in your Spring Boot application? 🤔 Don't worry, we've got your back! In this guide, we'll walk you through common issues and provide easy solutions to make Spring override all external config files on the classpath. Let's dive in! 💪

The Challenge: Overriding Default Properties 👊

So, you have a set of default properties located under /src/main/resources in your myapp.jar, and your springcontext expects these files to be on the classpath. Easy peasy! 😎 But, here's the twist: you need the flexibility to override these properties with an external set. You've tried setting the config folder in your current working directory (cwd), as per Spring Boot's documentation, but only the application.properties file gets picked up, while the rest continue to be fetched from /src/main/resources. 😬

The Workaround: Compromising Agility 🙇‍♂️

To work around this limitation, you might have already resorted to a workaround. You set the app.config.location (an app-specific property) through the command line when running your application. For example: 🏃‍♂️

java -jar myapp.jar app.config.location=file:./config

To adapt to this approach, you also made changes to your applicationcontext as follows: 👇

<util:properties id="Job1Props" location="{app.config.location}/job1.properties"></util:properties>

<util:properties id="Job2Props" location="{app.config.location}/job2.properties"></util:properties>

This separates file and classpath while loading the application. While this workaround gets the job done, it's not the most elegant solution, right? You still want to achieve the simplicity of overriding all external config files on the classpath, just like Spring does for the application.properties file. No more compromises! 😤

The Ultimate Solution: Let's Overcome Defaults! 🚀

To make Spring override all external config files on the classpath, we'll show you the light! 🌟 Here's how to achieve it in a few simple steps:

  1. First, remove the workaround of setting app.config.location and revert your applicationcontext back to the original state. We're going to solve it properly this time! 😉

<util:properties id="Job1Props" location="classpath:job1.properties"></util:properties>

<util:properties id="Job2Props" location="classpath:job2.properties"></util:properties>
  1. Create an external config folder in your project's root directory, next to your myapp.jar. You can name it anything you prefer, let's go with external-config for now. 📁

  2. Move all the property files you want to override from the default set (under /src/main/resources) to the newly created external config folder (external-config).

  3. Now, running your Spring Boot application with the spring.config.location system property set to the external config folder will ensure that all properties in that folder override the default ones.

java -jar myapp.jar -Dspring.config.location=file:./external-config/

🎉 Voilà! You've successfully made Spring override all external config files on the classpath, just like it does for application.properties! No more compromises or workarounds, pure agility and simplicity. 🙌

Engage with Us! 💌

We hope this guide helped you overcome the challenge of overriding default properties in your Spring Boot application. If you found this post valuable or have any questions, feel free to leave a comment below. We'd love to hear from you! Let's keep the conversation going. 👥💬

And don't forget to share this post with your fellow Spring Boot enthusiasts! Spread the knowledge and empower others to embrace the elegance of overriding multiple external config files. Together, we can make coding a breeze! 🌬️🚀

#HappyCoding


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello