About Daniel Bryant

Daniel Bryant spends his day working as a software development consultant, specialising in designing and deploying JVM and NoSQL-based business solutions to the cloud for Small-to-Medium Enterprises.

Overriding a packaged Spring application properties file via an external file

Quite a common use case when developing a Spring application is that you want to have multiple versions of configuration properties depending on where you are deploying to, for example, database URLs or feature flag may be environment specific on dev, qa, production etc.

Like most Spring development tasks, there are several way to solve the problem. My preference is based on the following assumptions and preferences:

  • We create a default configuration properties file (e.g. ‘appConfig.properties’) and package this within the deployable artifact (JAR or WAR etc)
    • This file will contain a sensible set of default ‘baseline’ properties that the application requires to run successfully
    • This default configuration file will typically be used for development purposes i.e. the  appConfig.properties included within the application code will allow the application to work ‘out of the box’ on a well configured local development box
    • We are using Maven to package our application, and therefore we place the appConfig.properties file in the root level of the src/main/resources directory – this doesn’t mean you have to use Maven, but you will have to ensure that your application packaging process includes the properties files in a location that is on the classpath
  • We want to override properties in the baseline appConfig.properties file via an external file located in the deployed application’s working directory
    • We typically name this file appConfigOverride.properties
    • We may not override all of the default properties, and therefore we want any properties not present in the override file to default to the values in the baseline appConfig.properties file
  • It is possible to override application properties by passing parameters on the Command Line when executing the application, or setting system variables, but this is a separate topic

The Solution

We use the following structure for our application-context.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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


http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="file:appConfigOverride.properties" order="-1"
                                  ignore-unresolvable="true" ignore-resource-not-found="true" />
    <context:property-placeholder location="classpath:appConfig.properties" />

    ....

</beans>

The key things here, are the ‘order’ attribute, which forces properties found in the appConfigOverride.properties to be used when a property is also found in another file (effectively overriding values from the other file), and the ‘ignore-unresolvable=”true” ignore-resource-not-found=”true”‘ which allows Spring to continue loading the context if it can’t find the external file (or it doesn’t contain override all of the default properties from the appConfig file)
 

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

3 Responses to "Overriding a packaged Spring application properties file via an external file"

  1. Neo says:

    Nice tip, Daniel.

    One more option that we might want to consider, when instead of static loading, we want to consider runtime configuration, we can use JMX as well to change the values.

    Thanks Neo,
    http://javawithneo.blogspot.com

  2. Niels says:

    Thanks for the tip, very useful! I was doing something similar, but whithout the order property.

  3. Craig says:

    Good article. We used this approach very successfully on a project which launched this year in the UK. Passing command line arguments when starting the application is too error-prone and should be avoided IMHO.

Leave a Reply


three − 1 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books