diff --git a/spring-all/src/main/java/org/baeldung/core/ComponentUsingProperties.java b/spring-all/src/main/java/org/baeldung/properties/core/ComponentUsingProperties.java similarity index 73% rename from spring-all/src/main/java/org/baeldung/core/ComponentUsingProperties.java rename to spring-all/src/main/java/org/baeldung/properties/core/ComponentUsingProperties.java index 7df4d8ead0..7e082702fe 100644 --- a/spring-all/src/main/java/org/baeldung/core/ComponentUsingProperties.java +++ b/spring-all/src/main/java/org/baeldung/properties/core/ComponentUsingProperties.java @@ -1,4 +1,4 @@ -package org.baeldung.core; +package org.baeldung.properties.core; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; @@ -23,8 +23,8 @@ public class ComponentUsingProperties implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { - System.out.println("via @Value: " + injectedProperty); - System.out.println("via Environment: " + env.getProperty("key.something")); + System.out.println("in afterPropertiesSet via @Value: " + injectedProperty); + System.out.println("in afterPropertiesSet Environment: " + env.getProperty("key.something")); } } diff --git a/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfig.java b/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfig.java new file mode 100644 index 0000000000..0269053fbe --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfig.java @@ -0,0 +1,25 @@ +package org.baeldung.properties.spring; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; + +@Configuration +@ComponentScan("org.baeldung.core") +@PropertySource("classpath:foo.properties") +public class PropertiesWithJavaConfig { + + public PropertiesWithJavaConfig() { + super(); + } + + // beans + + @Bean + public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } + +} \ No newline at end of file diff --git a/spring-all/src/main/java/org/baeldung/spring/properties/PropertiesWithXmlConfig.java b/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithXmlConfig.java similarity index 64% rename from spring-all/src/main/java/org/baeldung/spring/properties/PropertiesWithXmlConfig.java rename to spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithXmlConfig.java index f5f5de77cb..9ad7febcb0 100644 --- a/spring-all/src/main/java/org/baeldung/spring/properties/PropertiesWithXmlConfig.java +++ b/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithXmlConfig.java @@ -1,14 +1,13 @@ -package org.baeldung.spring.properties; +package org.baeldung.properties.spring; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration @ImportResource("classpath:configForProperties.xml") @ComponentScan("org.baeldung.core") -public class PropertiesWithXmlConfig extends WebMvcConfigurerAdapter { +public class PropertiesWithXmlConfig { public PropertiesWithXmlConfig() { super(); diff --git a/spring-all/src/main/java/org/baeldung/spring/properties/PropertiesWithJavaConfig.java b/spring-all/src/main/java/org/baeldung/spring/properties/PropertiesWithJavaConfig.java deleted file mode 100644 index fc67b7430a..0000000000 --- a/spring-all/src/main/java/org/baeldung/spring/properties/PropertiesWithJavaConfig.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.baeldung.spring.properties; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; - -@Configuration -@ComponentScan("org.baeldung.core") -@PropertySource("classpath:foo.properties") -public class PropertiesWithJavaConfig extends WebMvcConfigurerAdapter { - - public PropertiesWithJavaConfig() { - super(); - } - -} \ 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 c3218dffa4..6ec3d15fe5 100644 --- a/spring-all/src/main/resources/configForProperties.xml +++ b/spring-all/src/main/resources/configForProperties.xml @@ -1,9 +1,13 @@ - + + + \ No newline at end of file diff --git a/spring-all/src/main/resources/logback.xml b/spring-all/src/main/resources/logback.xml index 1146dade63..45c9697f77 100644 --- a/spring-all/src/main/resources/logback.xml +++ b/spring-all/src/main/resources/logback.xml @@ -12,6 +12,8 @@ + + diff --git a/spring-all/src/test/java/org/baeldung/core/PropertiesWithJavaIntegrationTest.java b/spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithJavaIntegrationTest.java similarity index 50% rename from spring-all/src/test/java/org/baeldung/core/PropertiesWithJavaIntegrationTest.java rename to spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithJavaIntegrationTest.java index e3cfb493ed..72d4ccb4af 100644 --- a/spring-all/src/test/java/org/baeldung/core/PropertiesWithJavaIntegrationTest.java +++ b/spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithJavaIntegrationTest.java @@ -1,8 +1,11 @@ -package org.baeldung.core; +package org.baeldung.properties.core; -import org.baeldung.spring.properties.PropertiesWithJavaConfig; +import org.baeldung.properties.spring.PropertiesWithJavaConfig; 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; @@ -11,9 +14,16 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; @ContextConfiguration(classes = { PropertiesWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class) public class PropertiesWithJavaIntegrationTest { + @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")); } } diff --git a/spring-all/src/test/java/org/baeldung/core/PropertiesWithXmlIntegrationTest.java b/spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithXmlIntegrationTest.java similarity index 50% rename from spring-all/src/test/java/org/baeldung/core/PropertiesWithXmlIntegrationTest.java rename to spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithXmlIntegrationTest.java index 44cacf58e3..ff5eaab910 100644 --- a/spring-all/src/test/java/org/baeldung/core/PropertiesWithXmlIntegrationTest.java +++ b/spring-all/src/test/java/org/baeldung/properties/core/PropertiesWithXmlIntegrationTest.java @@ -1,8 +1,11 @@ -package org.baeldung.core; +package org.baeldung.properties.core; -import org.baeldung.spring.properties.PropertiesWithXmlConfig; +import org.baeldung.properties.spring.PropertiesWithXmlConfig; 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; @@ -11,9 +14,16 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; @ContextConfiguration(classes = { PropertiesWithXmlConfig.class }, loader = AnnotationConfigContextLoader.class) public class PropertiesWithXmlIntegrationTest { + @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")); } }