diff --git a/spring-all/src/test/java/org/baeldung/properties/config/ChildConfig.java b/spring-all/src/test/java/org/baeldung/properties/config/ChildConfig.java deleted file mode 100644 index 6a33765748..0000000000 --- a/spring-all/src/test/java/org/baeldung/properties/config/ChildConfig.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.baeldung.properties.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; - -@Configuration -@PropertySource("classpath:child.properties") -public class ChildConfig { - -} diff --git a/spring-all/src/test/java/org/baeldung/properties/config/ParentConfig.java b/spring-all/src/test/java/org/baeldung/properties/config/ParentConfig.java deleted file mode 100644 index 2279af7803..0000000000 --- a/spring-all/src/test/java/org/baeldung/properties/config/ParentConfig.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.baeldung.properties.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; - -@Configuration -@PropertySource("classpath:parent.properties") -public class ParentConfig { - -} diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/ChildValueHolder.java b/spring-all/src/test/java/org/baeldung/properties/parentchild/ChildValueHolder.java new file mode 100644 index 0000000000..6df8fc31f0 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/properties/parentchild/ChildValueHolder.java @@ -0,0 +1,23 @@ +package org.baeldung.properties.parentchild; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class ChildValueHolder { + @Value("${parent.name:-}") + private String parentName; + + @Value("${child.name:-}") + private String childName; + + + public String getParentName() { + return parentName; + } + + public String getChildName() { + return childName; + } + +} diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertyPlaceHolderPropertiesTest.java b/spring-all/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertyPlaceHolderPropertiesTest.java new file mode 100644 index 0000000000..92af3f52f0 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertyPlaceHolderPropertiesTest.java @@ -0,0 +1,50 @@ +package org.baeldung.properties.parentchild; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.baeldung.properties.parentchild.config.ChildConfig2; +import org.baeldung.properties.parentchild.config.ParentConfig2; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.ContextHierarchy; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig2.class), @ContextConfiguration(classes = ChildConfig2.class) }) +public class ParentChildPropertyPlaceHolderPropertiesTest { + + @Autowired + private WebApplicationContext wac; + + @Test + public void givenPropertyPlaceHolder_whenGetPropertyUsingEnv_thenCorrect() { + final Environment childEnv = wac.getEnvironment(); + final Environment parentEnv = wac.getParent().getEnvironment(); + + assertNull(parentEnv.getProperty("parent.name")); + assertNull(parentEnv.getProperty("child.name")); + + assertNull(childEnv.getProperty("parent.name")); + assertNull(childEnv.getProperty("child.name")); + } + + @Test + public void givenPropertyPlaceHolder_whenGetPropertyUsingValueAnnotation_thenCorrect() { + final ChildValueHolder childValueHolder = wac.getBean(ChildValueHolder.class); + final ParentValueHolder parentValueHolder = wac.getParent().getBean(ParentValueHolder.class); + + assertEquals(parentValueHolder.getParentName(), "parent"); + assertEquals(parentValueHolder.getChildName(), "-"); + + assertEquals(childValueHolder.getParentName(), "-"); + assertEquals(childValueHolder.getChildName(), "child"); + } + +} diff --git a/spring-all/src/test/java/org/baeldung/properties/config/ParentChildPropertiesTest.java b/spring-all/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertySourcePropertiesTest.java similarity index 59% rename from spring-all/src/test/java/org/baeldung/properties/config/ParentChildPropertiesTest.java rename to spring-all/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertySourcePropertiesTest.java index 11c62e21b6..3ffd490c87 100644 --- a/spring-all/src/test/java/org/baeldung/properties/config/ParentChildPropertiesTest.java +++ b/spring-all/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertySourcePropertiesTest.java @@ -1,8 +1,10 @@ -package org.baeldung.properties.config; +package org.baeldung.properties.parentchild; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import org.baeldung.properties.parentchild.config.ChildConfig; +import org.baeldung.properties.parentchild.config.ParentConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -16,13 +18,13 @@ import org.springframework.web.context.WebApplicationContext; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextHierarchy({ @ContextConfiguration(classes = ParentConfig.class), @ContextConfiguration(classes = ChildConfig.class) }) -public class ParentChildPropertiesTest { +public class ParentChildPropertySourcePropertiesTest { @Autowired private WebApplicationContext wac; @Test - public void givenParentPropertySource_whenGetValue_thenCorrect() { + public void givenPropertySource_whenGetPropertyUsingEnv_thenCorrect() { final Environment childEnv = wac.getEnvironment(); final Environment parentEnv = wac.getParent().getEnvironment(); @@ -32,4 +34,17 @@ public class ParentChildPropertiesTest { assertEquals(childEnv.getProperty("parent.name"), "parent"); assertEquals(childEnv.getProperty("child.name"), "child"); } + + @Test + public void givenPropertySource_whenGetPropertyUsingValueAnnotation_thenCorrect() { + final ChildValueHolder childValueHolder = wac.getBean(ChildValueHolder.class); + final ParentValueHolder parentValueHolder = wac.getParent().getBean(ParentValueHolder.class); + + assertEquals(parentValueHolder.getParentName(), "parent"); + assertEquals(parentValueHolder.getChildName(), "-"); + + assertEquals(childValueHolder.getParentName(), "parent"); + assertEquals(childValueHolder.getChildName(), "child"); + } + } diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/ParentValueHolder.java b/spring-all/src/test/java/org/baeldung/properties/parentchild/ParentValueHolder.java new file mode 100644 index 0000000000..fef9a614dc --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/properties/parentchild/ParentValueHolder.java @@ -0,0 +1,23 @@ +package org.baeldung.properties.parentchild; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class ParentValueHolder { + @Value("${parent.name:-}") + private String parentName; + + @Value("${child.name:-}") + private String childName; + + + public String getParentName() { + return parentName; + } + + public String getChildName() { + return childName; + } + +} diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig.java b/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig.java new file mode 100644 index 0000000000..23c8db7864 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig.java @@ -0,0 +1,21 @@ +package org.baeldung.properties.parentchild.config; + +import org.baeldung.properties.parentchild.ChildValueHolder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; + +@Configuration +@PropertySource("classpath:child.properties") +public class ChildConfig { + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } + + @Bean + public ChildValueHolder childValueHolder() { + return new ChildValueHolder(); + } +} diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig2.java b/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig2.java new file mode 100644 index 0000000000..2ed14d7701 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig2.java @@ -0,0 +1,23 @@ +package org.baeldung.properties.parentchild.config; + +import org.baeldung.properties.parentchild.ChildValueHolder; +import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; + +@Configuration +public class ChildConfig2 { + + @Bean + public ChildValueHolder childValueHolder() { + return new ChildValueHolder(); + } + + @Bean + public static PropertyPlaceholderConfigurer configurer() { + final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); + ppc.setLocations(new ClassPathResource("child.properties")); + return ppc; + } +} diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig.java b/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig.java new file mode 100644 index 0000000000..ed407e0b52 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig.java @@ -0,0 +1,23 @@ +package org.baeldung.properties.parentchild.config; + +import org.baeldung.properties.parentchild.ParentValueHolder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; + +@Configuration +@PropertySource("classpath:parent.properties") +public class ParentConfig { + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } + + @Bean + public ParentValueHolder parentValueHolder() { + return new ParentValueHolder(); + } + +} diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig2.java b/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig2.java new file mode 100644 index 0000000000..c38ba8e483 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig2.java @@ -0,0 +1,23 @@ +package org.baeldung.properties.parentchild.config; + +import org.baeldung.properties.parentchild.ParentValueHolder; +import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; + +@Configuration +public class ParentConfig2 { + + @Bean + public ParentValueHolder parentValueHolder() { + return new ParentValueHolder(); + } + + @Bean + public static PropertyPlaceholderConfigurer configurer() { + final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); + ppc.setLocations(new ClassPathResource("parent.properties")); + return ppc; + } +}