From 3fe623dcabeba47b36fe5d50b7c1ff21ece4afe9 Mon Sep 17 00:00:00 2001 From: PranayJain Date: Wed, 27 Feb 2019 15:00:02 +0530 Subject: [PATCH] BAEL-2719: Spring Boot - Properties file outside jar --- .../ExternalPropertyFileLoader.java | 17 +++++++++++++ .../main/resources/external/conf.properties | 4 +++ .../ExternalPropertyFileLoaderUnitTest.java | 25 +++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyFileLoader.java create mode 100644 spring-boot-ops/src/main/resources/external/conf.properties create mode 100644 spring-boot-ops/src/test/java/com/baeldung/properties/ExternalPropertyFileLoaderUnitTest.java diff --git a/spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyFileLoader.java b/spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyFileLoader.java new file mode 100644 index 0000000000..233ddc0195 --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyFileLoader.java @@ -0,0 +1,17 @@ +package com.baeldung.properties; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; + +@SpringBootApplication +public class ExternalPropertyFileLoader { + public static void main(String[] args) { + ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(ExternalPropertyFileLoader.class).properties("spring.config.name:conf", "spring.config.location:file:src/main/resources/external/") + .build() + .run(args); + ConfigurableEnvironment environment = applicationContext.getEnvironment(); + environment.getProperty("username"); + } +} diff --git a/spring-boot-ops/src/main/resources/external/conf.properties b/spring-boot-ops/src/main/resources/external/conf.properties new file mode 100644 index 0000000000..a724b878b4 --- /dev/null +++ b/spring-boot-ops/src/main/resources/external/conf.properties @@ -0,0 +1,4 @@ +url=jdbc:postgresql://localhost:5432/ +username=admin +password=root +spring.main.allow-bean-definition-overriding=true diff --git a/spring-boot-ops/src/test/java/com/baeldung/properties/ExternalPropertyFileLoaderUnitTest.java b/spring-boot-ops/src/test/java/com/baeldung/properties/ExternalPropertyFileLoaderUnitTest.java new file mode 100644 index 0000000000..656d8561ec --- /dev/null +++ b/spring-boot-ops/src/test/java/com/baeldung/properties/ExternalPropertyFileLoaderUnitTest.java @@ -0,0 +1,25 @@ +package com.baeldung.properties; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +public class ExternalPropertyFileLoaderUnitTest { + + @Test + public void whenExternalisedPropertiesLoaded_thenReadValues() { + ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(ExternalPropertyFileLoader.class).properties("spring.config.name:conf", "spring.config.location:file:src/main/resources/external/") + .build() + .run(); + ConfigurableEnvironment environment = applicationContext.getEnvironment(); + Assert.assertEquals(environment.getProperty("url"), "jdbc:postgresql://localhost:5432/"); + Assert.assertEquals(environment.getProperty("username"), "admin"); + Assert.assertEquals(environment.getProperty("password"), "root"); + } + +}