From 9722a641271389fd9f79c2f30b6780d7f4e09c6e Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 1 Jun 2013 13:59:11 +0300 Subject: [PATCH] properties work --- .../spring/PropertiesWithJavaConfigOther.java | 16 ++++++++++ .../spring/PropertiesWithXmlConfigOne.java | 16 ++++++++++ .../spring/PropertiesWithXmlConfigTwo.java | 14 +++++++++ spring-all/src/main/resources/bar.properties | 1 + .../main/resources/configForProperties.xml | 5 +--- .../main/resources/configForPropertiesOne.xml | 15 ++++++++++ .../main/resources/configForPropertiesTwo.xml | 11 +++++++ .../PropertiesWithJavaIntegrationTest.java | 3 +- ...ertiesWithMultipleXmlsIntegrationTest.java | 30 +++++++++++++++++++ 9 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfigOther.java create mode 100644 spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithXmlConfigOne.java create mode 100644 spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithXmlConfigTwo.java create mode 100644 spring-all/src/main/resources/bar.properties create mode 100644 spring-all/src/main/resources/configForPropertiesOne.xml create mode 100644 spring-all/src/main/resources/configForPropertiesTwo.xml create mode 100644 spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithMultipleXmlsIntegrationTest.java diff --git a/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfigOther.java b/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfigOther.java new file mode 100644 index 0000000000..594ba0a09d --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfigOther.java @@ -0,0 +1,16 @@ +package org.baeldung.properties.spring; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +@Configuration +@PropertySource("classpath:bar.properties") +public class PropertiesWithJavaConfigOther { + + public PropertiesWithJavaConfigOther() { + super(); + } + + // beans + +} \ No newline at end of file diff --git a/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithXmlConfigOne.java b/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithXmlConfigOne.java new file mode 100644 index 0000000000..9061cc10d4 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithXmlConfigOne.java @@ -0,0 +1,16 @@ +package org.baeldung.properties.spring; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + +@Configuration +@ImportResource("classpath:configForPropertiesOne.xml") +@ComponentScan("org.baeldung.core") +public class PropertiesWithXmlConfigOne { + + public PropertiesWithXmlConfigOne() { + super(); + } + +} \ No newline at end of file diff --git a/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithXmlConfigTwo.java b/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithXmlConfigTwo.java new file mode 100644 index 0000000000..e4365cbc8b --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithXmlConfigTwo.java @@ -0,0 +1,14 @@ +package org.baeldung.properties.spring; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + +@Configuration +@ImportResource("classpath:configForPropertiesTwo.xml") +public class PropertiesWithXmlConfigTwo { + + public PropertiesWithXmlConfigTwo() { + super(); + } + +} \ No newline at end of file diff --git a/spring-all/src/main/resources/bar.properties b/spring-all/src/main/resources/bar.properties new file mode 100644 index 0000000000..1a41a49c4c --- /dev/null +++ b/spring-all/src/main/resources/bar.properties @@ -0,0 +1 @@ +key.something2=val2 \ No newline at end of file diff --git a/spring-all/src/main/resources/configForProperties.xml b/spring-all/src/main/resources/configForProperties.xml index fc6cf21069..20a40a3195 100644 --- a/spring-all/src/main/resources/configForProperties.xml +++ b/spring-all/src/main/resources/configForProperties.xml @@ -6,12 +6,9 @@ http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> - - - - + diff --git a/spring-all/src/main/resources/configForPropertiesOne.xml b/spring-all/src/main/resources/configForPropertiesOne.xml new file mode 100644 index 0000000000..ad9716fada --- /dev/null +++ b/spring-all/src/main/resources/configForPropertiesOne.xml @@ -0,0 +1,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/spring-all/src/main/resources/configForPropertiesTwo.xml b/spring-all/src/main/resources/configForPropertiesTwo.xml new file mode 100644 index 0000000000..b9d5e86c95 --- /dev/null +++ b/spring-all/src/main/resources/configForPropertiesTwo.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithJavaIntegrationTest.java b/spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithJavaIntegrationTest.java index 72d4ccb4af..d6c99502d7 100644 --- a/spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithJavaIntegrationTest.java +++ b/spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithJavaIntegrationTest.java @@ -1,6 +1,7 @@ package org.baeldung.properties.core; import org.baeldung.properties.spring.PropertiesWithJavaConfig; +import org.baeldung.properties.spring.PropertiesWithJavaConfigOther; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -11,7 +12,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { PropertiesWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class) +@ContextConfiguration(classes = { PropertiesWithJavaConfig.class, PropertiesWithJavaConfigOther.class }, loader = AnnotationConfigContextLoader.class) public class PropertiesWithJavaIntegrationTest { @Autowired diff --git a/spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithMultipleXmlsIntegrationTest.java b/spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithMultipleXmlsIntegrationTest.java new file mode 100644 index 0000000000..9fc793fc1b --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithMultipleXmlsIntegrationTest.java @@ -0,0 +1,30 @@ +package org.baeldung.properties.core; + +import org.baeldung.properties.spring.PropertiesWithXmlConfigOne; +import org.baeldung.properties.spring.PropertiesWithXmlConfigTwo; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PropertiesWithXmlConfigOne.class, PropertiesWithXmlConfigTwo.class }, loader = AnnotationConfigContextLoader.class) +public class PropertiesWithMultipleXmlsIntegrationTest { + + @Autowired + private Environment env; + + @Value("${key.something}") + private String injectedProperty; + + @Test + public final void givenContextIsInitialized_thenNoException() { + System.out.println("in test via @Value: " + injectedProperty); + System.out.println("in test Environment: " + env.getProperty("key.something")); + } + +}