From 978ddb28065ef4765df6fa9c6959a5dda0aba8cd Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Mon, 5 Oct 2020 23:35:05 +0300 Subject: [PATCH] Implement support for additional properties files Signed-off-by: Pekka Helenius --- .../config/AdditionalPropertiesConfig.java | 69 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 3 + .../main/resources/authentication.properties | 4 ++ .../src/main/resources/website.properties | 37 ++++++++++ 4 files changed, 113 insertions(+) create mode 100644 bookstore/src/main/java/com/fjordtek/bookstore/config/AdditionalPropertiesConfig.java create mode 100644 bookstore/src/main/resources/META-INF/spring.factories create mode 100644 bookstore/src/main/resources/authentication.properties create mode 100644 bookstore/src/main/resources/website.properties diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/config/AdditionalPropertiesConfig.java b/bookstore/src/main/java/com/fjordtek/bookstore/config/AdditionalPropertiesConfig.java new file mode 100644 index 0000000..9f793ec --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/config/AdditionalPropertiesConfig.java @@ -0,0 +1,69 @@ +//Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.config; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertiesPropertySource; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PropertiesLoaderUtils; + +/** +* +* This class implements a custom EnvironmentPostProcessor interface. +*

+* Main purpose of this implementation is to load additional properties files into Spring +* environment as early as possible during Spring Boot initialization process. +*

+* Loading of this class must be enabled in /resources/META-INF/spring.factories file. +* +* @author Pekka Helenius +*/ + +@Configuration +//@Order(Ordered.LOWEST_PRECEDENCE) +public class AdditionalPropertiesConfig implements EnvironmentPostProcessor { + + private List getAdditionalResources() { + List resources = new ArrayList(); + + // Add your additional properties files here + resources.add(new ClassPathResource("website.properties")); + resources.add(new ClassPathResource("authentication.properties")); + + return resources; + } + + @Override + public void postProcessEnvironment( + ConfigurableEnvironment environment, + SpringApplication application + ) { + + MutablePropertySources propertySources = environment.getPropertySources(); + + for (Resource res : getAdditionalResources()) { + try { + propertySources.addLast( + new PropertiesPropertySource( + res.getFilename(), + PropertiesLoaderUtils.loadProperties(res) + ) + ); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + +} + diff --git a/bookstore/src/main/resources/META-INF/spring.factories b/bookstore/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..c44fda8 --- /dev/null +++ b/bookstore/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +# Pekka Helenius , Fjordtek 2020 + +org.springframework.boot.env.EnvironmentPostProcessor = com.fjordtek.bookstore.config.AdditionalPropertiesConfig \ No newline at end of file diff --git a/bookstore/src/main/resources/authentication.properties b/bookstore/src/main/resources/authentication.properties new file mode 100644 index 0000000..2bbc77e --- /dev/null +++ b/bookstore/src/main/resources/authentication.properties @@ -0,0 +1,4 @@ +# Pekka Helenius , Fjordtek 2020 + +auth.field.username = b_username +auth.field.password = b_password \ No newline at end of file diff --git a/bookstore/src/main/resources/website.properties b/bookstore/src/main/resources/website.properties new file mode 100644 index 0000000..b2e3df3 --- /dev/null +++ b/bookstore/src/main/resources/website.properties @@ -0,0 +1,37 @@ +# Pekka Helenius , Fjordtek 2020 + +# BookController +# Public end points +# +page.url.index = / +page.url.login = /login +page.url.logout = /logout + +page.url.list = /booklist +page.url.add = /bookadd +page.url.edit = /bookedit +page.url.delete = /bookdelete + +page.url.apiref = /apiref +page.url.autherror = /autherror + +page.url.error = /error + +# BookRestController +# Public JSON end points +# +page.url.json = /json +page.url.json.list = ${page.url.list} +page.url.json.book = /book + + +# BookBasePathAwareController +# Native REST API end points +# +page.url.restapi.list = /${page.url.list} + +# Bookstore assets & resources +# +page.url.resources.css = /css +page.url.resources.js = /js +page.url.resources.images = /images \ No newline at end of file