From 3fe623dcabeba47b36fe5d50b7c1ff21ece4afe9 Mon Sep 17 00:00:00 2001 From: PranayJain Date: Wed, 27 Feb 2019 15:00:02 +0530 Subject: [PATCH 1/3] 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"); + } + +} From 1b48e7943c2942feb1d49b8e4a8a352315a62fa4 Mon Sep 17 00:00:00 2001 From: PranayJain Date: Sun, 10 Mar 2019 17:30:20 +0530 Subject: [PATCH 2/3] BAEL-2719: Fixed the changes to load external file --- spring-boot-ops/pom.xml | 23 ++++++++++ .../baeldung/properties/ConfProperties.java | 43 +++++++++++++++++++ .../ExternalPropertyConfigurer.java | 18 ++++++++ .../ExternalPropertyFileLoader.java | 15 ++++--- .../main/resources/external/conf.properties | 4 -- ...rnalPropertyFileLoaderIntegrationTest.java | 27 ++++++++++++ .../ExternalPropertyFileLoaderUnitTest.java | 25 ----------- .../test/resources/external/conf.properties | 3 ++ 8 files changed, 122 insertions(+), 36 deletions(-) create mode 100644 spring-boot-ops/src/main/java/com/baeldung/properties/ConfProperties.java create mode 100644 spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyConfigurer.java delete mode 100644 spring-boot-ops/src/main/resources/external/conf.properties create mode 100644 spring-boot-ops/src/test/java/com/baeldung/properties/ExternalPropertyFileLoaderIntegrationTest.java delete mode 100644 spring-boot-ops/src/test/java/com/baeldung/properties/ExternalPropertyFileLoaderUnitTest.java create mode 100644 spring-boot-ops/src/test/resources/external/conf.properties diff --git a/spring-boot-ops/pom.xml b/spring-boot-ops/pom.xml index 625b2ad188..6c49351820 100644 --- a/spring-boot-ops/pom.xml +++ b/spring-boot-ops/pom.xml @@ -110,6 +110,29 @@ + + org.apache.maven.plugins + maven-failsafe-plugin + 2.18 + + + + integration-tests + + integration-test + verify + + + + + **/ExternalPropertyFileLoaderIntegrationTest.java + + + + + diff --git a/spring-boot-ops/src/main/java/com/baeldung/properties/ConfProperties.java b/spring-boot-ops/src/main/java/com/baeldung/properties/ConfProperties.java new file mode 100644 index 0000000000..0b6041bb06 --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/properties/ConfProperties.java @@ -0,0 +1,43 @@ +package com.baeldung.properties; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class ConfProperties { + + @Value("${url}") + private String url; + + @Value("${username}") + private String username; + + @Value("${password}") + private String password; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + +} diff --git a/spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyConfigurer.java b/spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyConfigurer.java new file mode 100644 index 0000000000..67890d717a --- /dev/null +++ b/spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyConfigurer.java @@ -0,0 +1,18 @@ +package com.baeldung.properties; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.core.io.FileSystemResource; + +@Configuration +public class ExternalPropertyConfigurer { + + @Bean + public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer(); + properties.setLocation(new FileSystemResource("src/test/resources/external/conf.properties")); + properties.setIgnoreResourceNotFound(false); + return properties; + } +} \ No newline at end of file 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 index 233ddc0195..dc5e14549b 100644 --- a/spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyFileLoader.java +++ b/spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyFileLoader.java @@ -1,17 +1,18 @@ package com.baeldung.properties; +import org.springframework.beans.factory.annotation.Autowired; 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 { + + @Autowired + ConfProperties prop; + 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"); + new SpringApplicationBuilder(ExternalPropertyFileLoader.class).build() + .run(args); } + } diff --git a/spring-boot-ops/src/main/resources/external/conf.properties b/spring-boot-ops/src/main/resources/external/conf.properties deleted file mode 100644 index a724b878b4..0000000000 --- a/spring-boot-ops/src/main/resources/external/conf.properties +++ /dev/null @@ -1,4 +0,0 @@ -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/ExternalPropertyFileLoaderIntegrationTest.java b/spring-boot-ops/src/test/java/com/baeldung/properties/ExternalPropertyFileLoaderIntegrationTest.java new file mode 100644 index 0000000000..2001db57d0 --- /dev/null +++ b/spring-boot-ops/src/test/java/com/baeldung/properties/ExternalPropertyFileLoaderIntegrationTest.java @@ -0,0 +1,27 @@ +package com.baeldung.properties; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = ExternalPropertyFileLoader.class) +public class ExternalPropertyFileLoaderIntegrationTest { + + @Autowired + ConfProperties props; + + @Test + public void whenExternalisedPropertiesLoaded_thenReadValues() throws IOException { + assertEquals("jdbc:postgresql://localhost:5432/", props.getUrl()); + assertEquals("admin", props.getUsername()); + assertEquals("root", props.getPassword()); + } + +} 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 deleted file mode 100644 index 656d8561ec..0000000000 --- a/spring-boot-ops/src/test/java/com/baeldung/properties/ExternalPropertyFileLoaderUnitTest.java +++ /dev/null @@ -1,25 +0,0 @@ -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"); - } - -} diff --git a/spring-boot-ops/src/test/resources/external/conf.properties b/spring-boot-ops/src/test/resources/external/conf.properties new file mode 100644 index 0000000000..da9533fe1f --- /dev/null +++ b/spring-boot-ops/src/test/resources/external/conf.properties @@ -0,0 +1,3 @@ +url=jdbc:postgresql://localhost:5432/ +username=admin +password=root \ No newline at end of file From dcaf1b35a049195e74c4a1c8b7ac719ce7f3f346 Mon Sep 17 00:00:00 2001 From: PranayJain Date: Mon, 11 Mar 2019 13:04:19 +0530 Subject: [PATCH 3/3] BAEL-2719: Skipped conf.properties from packaging --- spring-boot-ops/pom.xml | 9 +++++++++ .../baeldung/properties/ExternalPropertyConfigurer.java | 2 +- .../src/main/resources/external/conf.properties | 4 ++++ .../src/test/resources/external/conf.properties | 3 --- 4 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 spring-boot-ops/src/main/resources/external/conf.properties delete mode 100644 spring-boot-ops/src/test/resources/external/conf.properties diff --git a/spring-boot-ops/pom.xml b/spring-boot-ops/pom.xml index 6c49351820..f36434b682 100644 --- a/spring-boot-ops/pom.xml +++ b/spring-boot-ops/pom.xml @@ -95,6 +95,15 @@ ${project.artifactId} + + + src/main/resources + true + + **/conf.properties + + + org.springframework.boot diff --git a/spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyConfigurer.java b/spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyConfigurer.java index 67890d717a..0cdbb452d5 100644 --- a/spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyConfigurer.java +++ b/spring-boot-ops/src/main/java/com/baeldung/properties/ExternalPropertyConfigurer.java @@ -11,7 +11,7 @@ public class ExternalPropertyConfigurer { @Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer(); - properties.setLocation(new FileSystemResource("src/test/resources/external/conf.properties")); + properties.setLocation(new FileSystemResource("src/main/resources/external/conf.properties")); properties.setIgnoreResourceNotFound(false); return properties; } 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..cfcd23dc76 --- /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 \ No newline at end of file diff --git a/spring-boot-ops/src/test/resources/external/conf.properties b/spring-boot-ops/src/test/resources/external/conf.properties deleted file mode 100644 index da9533fe1f..0000000000 --- a/spring-boot-ops/src/test/resources/external/conf.properties +++ /dev/null @@ -1,3 +0,0 @@ -url=jdbc:postgresql://localhost:5432/ -username=admin -password=root \ No newline at end of file