Skip to content

Instantly share code, notes, and snippets.

@andrewrlee
Last active August 29, 2015 13:57
Show Gist options
  • Save andrewrlee/9535587 to your computer and use it in GitHub Desktop.
Save andrewrlee/9535587 to your computer and use it in GitHub Desktop.
Overriding property files in spring

Overriding property files in spring

I think the trick is to just use one property place holder and I think this covers all of your scenarios:

It resolves ${full.url} in this way:

  • first default.properties gets loaded => full.url=http://prod:9090
  • then dev-test.properties overrides the hostname and port name => full.url=http://test:8080
  • then development properties overrides the hostname only => full.url=http://localhost:8080

It also doesn't blow up that it can't find "override-I-Dont-Exist.properties" file but if we added it then it could override any property.

It also blows up if we tried to find a property called ${iDontExist}

One PropertyPlaceholderConfigurer can cope with strange property dependency cycles across it's own files, but if you have two, then they can't cope with references to properties in each other's files.

package uk.co.optimisticpanda;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/applicationContext.xml")
public class ConfigurationTest {
@Autowired
private Bean bean;
@Test
public void checkingOverrides(){
assertThat(bean.getProperty(), is("http://localhost:8080/service"));
}
public static class Bean {
private String property;
public Bean(String property){
this.property = property;
}
public String getProperty() {
return property;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
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-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<context:annotation-config />
<!-- later files in the list override earlier ones -->
<context:property-placeholder location="
classpath:prod.properties,
classpath:test.properties,
classpath:dev.properties,
classpath:override-I-Dont-Exist.properties,
"
ignore-resource-not-found="true" ignore-unresolvable="false"/>
<bean class="uk.co.optimisticpanda.ConfigurationTest.Bean" c:_0="${full.url}" />
</beans>
service.host=prod
service.port=9090
service.hostAndPort=${service.host}:${service.port}
full.url=http://${service.hostAndPort}/service
service.port=8080
service.host=test
service.host=localhost
<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>org.springframework.samples</groupId>
<artifactId>config-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- Generic properties -->
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment