From e09ba8e3c2afc2ca27587e543c9328a56222ebde Mon Sep 17 00:00:00 2001 From: adasioo Date: Fri, 19 May 2017 09:10:21 +0200 Subject: [PATCH] BAEL-923 How to inject a value from properties to a class not managed by Spring? (#1878) * adam.zawada@gmail.com - Different Types of Bean Injection in Spring * adam.zawada@gmail.com - Different Types of Bean Injection in Spring switch to Java based configuration * BAEL-895 calculate the period/duration between two dates in Java 8 * clean old PR * BAEL-923 How to inject a value from properties to a class not managed by Spring? * clean PR --- .../com/baeldung/util/PropertiesLoader.java | 19 +++++++++++ .../baeldung/util/PropertiesLoaderTest.java | 34 +++++++++++++++++++ .../test/resources/configuration.properties | 4 +++ 3 files changed, 57 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/util/PropertiesLoader.java create mode 100644 core-java/src/test/java/com/baeldung/util/PropertiesLoaderTest.java create mode 100644 core-java/src/test/resources/configuration.properties diff --git a/core-java/src/main/java/com/baeldung/util/PropertiesLoader.java b/core-java/src/main/java/com/baeldung/util/PropertiesLoader.java new file mode 100644 index 0000000000..78f670913e --- /dev/null +++ b/core-java/src/main/java/com/baeldung/util/PropertiesLoader.java @@ -0,0 +1,19 @@ +package com.baeldung.util; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class PropertiesLoader { + + public static Properties loadProperties(String resourceFileName) throws IOException { + Properties configuration = new Properties(); + InputStream inputStream = PropertiesLoader.class + .getClassLoader() + .getResourceAsStream(resourceFileName); + configuration.load(inputStream); + inputStream.close(); + return configuration; + } + +} diff --git a/core-java/src/test/java/com/baeldung/util/PropertiesLoaderTest.java b/core-java/src/test/java/com/baeldung/util/PropertiesLoaderTest.java new file mode 100644 index 0000000000..fa3c425156 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/util/PropertiesLoaderTest.java @@ -0,0 +1,34 @@ +package com.baeldung.util; + +import org.junit.Test; + +import java.io.IOException; +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + +public class PropertiesLoaderTest { + + @Test + public void loadProperties_whenPropertyReaded_thenSuccess() throws IOException { + //given + final String RESOURCE_FILE_NAME = "configuration.properties"; + + final String SAMPLE_CONF_ENTRY = "sampleConfEntry"; + final String COLON_SEPARATED_CONF_ENTRY = "colonSeparatedEntry"; + + final String GIVEN_CONF_ENTRY_VALUE = "sample String value"; + final String COLON_SEPARATED_CONF_ENTRY_VALUE = "colon separated entry value"; + + //when + Properties config = PropertiesLoader.loadProperties(RESOURCE_FILE_NAME); + + String sampleConfEntryValue = config.getProperty(SAMPLE_CONF_ENTRY); + String colonSeparatedConfEntryValue = config.getProperty(COLON_SEPARATED_CONF_ENTRY); + + //then + assertEquals(GIVEN_CONF_ENTRY_VALUE, sampleConfEntryValue); + assertEquals(COLON_SEPARATED_CONF_ENTRY_VALUE, colonSeparatedConfEntryValue); + + } +} diff --git a/core-java/src/test/resources/configuration.properties b/core-java/src/test/resources/configuration.properties new file mode 100644 index 0000000000..338be84946 --- /dev/null +++ b/core-java/src/test/resources/configuration.properties @@ -0,0 +1,4 @@ +# this is sample property file for PropertiesLoaderTest configuration needs +! this is also a comment +sampleConfEntry = sample String value +colonSeparatedEntry : colon separated entry value