diff --git a/spring-all/README.md b/spring-all/README.md index d3296bd457..22c51525f3 100644 --- a/spring-all/README.md +++ b/spring-all/README.md @@ -11,7 +11,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant articles: - [Guide to Spring @Autowired](http://www.baeldung.com/spring-autowire) -- [Properties with Spring and Spring Boot](http://www.baeldung.com/properties-with-spring) - checkout the `org.baeldung.properties` package for all scenarios of properties injection and usage - [Spring Profiles](http://www.baeldung.com/spring-profiles) - [A Spring Custom Annotation for a Better DAO](http://www.baeldung.com/spring-annotation-bean-pre-processor) - [What’s New in Spring 4.3?](http://www.baeldung.com/whats-new-in-spring-4-3) diff --git a/spring-boot-properties/README.md b/spring-boot-properties/README.md index c43cf4865c..e467e129dd 100644 --- a/spring-boot-properties/README.md +++ b/spring-boot-properties/README.md @@ -1,2 +1,10 @@ ### Relevant Articles: -- [Reloading Properties in Spring](https://www.baeldung.com/reloading-properties-files-in-spring/) \ No newline at end of file +- [Reloading Properties in Spring](https://www.baeldung.com/reloading-properties-files-in-spring/) +- [Guide to @ConfigurationProperties in Spring Boot](http://www.baeldung.com/configuration-properties-in-spring-boot) +- [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties) +- [Guide to @EnableConfigurationProperties](https://www.baeldung.com/spring-enable-config-properties) +- [Properties with Spring and Spring Boot](http://www.baeldung.com/properties-with-spring) - checkout the `org.baeldung.properties` package for all scenarios of properties injection and usage +- [A Quick Guide to Spring @Value](http://www.baeldung.com/spring-value-annotation) +- [Spring YAML Configuration](http://www.baeldung.com/spring-yaml) +- [Using Spring @Value with Defaults](http://www.baeldung.com/spring-value-defaults) +- [How to Inject a Property Value Into a Class Not Managed by Spring?](http://www.baeldung.com/inject-properties-value-non-spring-class) \ No newline at end of file diff --git a/spring-boot-properties/pom.xml b/spring-boot-properties/pom.xml index 27ac48252b..ccb9204d96 100644 --- a/spring-boot-properties/pom.xml +++ b/spring-boot-properties/pom.xml @@ -37,6 +37,16 @@ spring-boot-starter-test test + + com.google.guava + guava + ${guava.version} + + + org.apache.httpcomponents + httpcore + ${httpcore.version} + @@ -99,6 +109,8 @@ 1.8 Greenwich.SR1 1.10 + 20.0 + 4.4.11 diff --git a/spring-boot/src/main/java/org/baeldung/properties/AdditionalConfiguration.java b/spring-boot-properties/src/main/java/com/baeldung/properties/AdditionalConfiguration.java similarity index 92% rename from spring-boot/src/main/java/org/baeldung/properties/AdditionalConfiguration.java rename to spring-boot-properties/src/main/java/com/baeldung/properties/AdditionalConfiguration.java index 499666c143..c5ccd20392 100644 --- a/spring-boot/src/main/java/org/baeldung/properties/AdditionalConfiguration.java +++ b/spring-boot-properties/src/main/java/com/baeldung/properties/AdditionalConfiguration.java @@ -1,4 +1,4 @@ -package org.baeldung.properties; +package com.baeldung.properties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; diff --git a/spring-boot/src/main/java/org/baeldung/properties/AdditionalProperties.java b/spring-boot-properties/src/main/java/com/baeldung/properties/AdditionalProperties.java similarity index 80% rename from spring-boot/src/main/java/org/baeldung/properties/AdditionalProperties.java rename to spring-boot-properties/src/main/java/com/baeldung/properties/AdditionalProperties.java index 64e39b1475..6f6842e5e9 100644 --- a/spring-boot/src/main/java/org/baeldung/properties/AdditionalProperties.java +++ b/spring-boot-properties/src/main/java/com/baeldung/properties/AdditionalProperties.java @@ -1,7 +1,9 @@ -package org.baeldung.properties; +package com.baeldung.properties; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +@Configuration @ConfigurationProperties(prefix = "additional") public class AdditionalProperties { diff --git a/spring-boot/src/main/java/com/baeldung/properties/ConfigProperties.java b/spring-boot-properties/src/main/java/com/baeldung/properties/ConfigProperties.java similarity index 88% rename from spring-boot/src/main/java/com/baeldung/properties/ConfigProperties.java rename to spring-boot-properties/src/main/java/com/baeldung/properties/ConfigProperties.java index 35b0239287..b690c8e305 100644 --- a/spring-boot/src/main/java/com/baeldung/properties/ConfigProperties.java +++ b/spring-boot-properties/src/main/java/com/baeldung/properties/ConfigProperties.java @@ -10,6 +10,7 @@ import javax.validation.constraints.Pattern; import org.hibernate.validator.constraints.Length; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.validation.annotation.Validated; @@ -54,7 +55,7 @@ public class ConfigProperties { } @NotBlank - private String host; + private String hostName; @Min(1025) @Max(65536) @@ -67,12 +68,12 @@ public class ConfigProperties { private List defaultRecipients; private Map additionalHeaders; - public String getHost() { - return host; + public String getHostName() { + return hostName; } - public void setHost(String host) { - this.host = host; + public void setHostName(String hostName) { + this.hostName = hostName; } public int getPort() { @@ -114,4 +115,10 @@ public class ConfigProperties { public void setAdditionalHeaders(Map additionalHeaders) { this.additionalHeaders = additionalHeaders; } + + @Bean + @ConfigurationProperties(prefix = "item") + public Item item(){ + return new Item(); + } } diff --git a/spring-boot/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java b/spring-boot-properties/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java rename to spring-boot-properties/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java diff --git a/spring-boot/src/main/java/com/baeldung/properties/CustomJsonProperties.java b/spring-boot-properties/src/main/java/com/baeldung/properties/CustomJsonProperties.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/properties/CustomJsonProperties.java rename to spring-boot-properties/src/main/java/com/baeldung/properties/CustomJsonProperties.java diff --git a/spring-boot/src/main/java/org/baeldung/properties/Item.java b/spring-boot-properties/src/main/java/com/baeldung/properties/Item.java similarity index 93% rename from spring-boot/src/main/java/org/baeldung/properties/Item.java rename to spring-boot-properties/src/main/java/com/baeldung/properties/Item.java index 0314654ada..741bbba144 100644 --- a/spring-boot/src/main/java/org/baeldung/properties/Item.java +++ b/spring-boot-properties/src/main/java/com/baeldung/properties/Item.java @@ -1,4 +1,4 @@ -package org.baeldung.properties; +package com.baeldung.properties; public class Item { diff --git a/spring-boot/src/main/java/com/baeldung/properties/JsonProperties.java b/spring-boot-properties/src/main/java/com/baeldung/properties/JsonProperties.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/properties/JsonProperties.java rename to spring-boot-properties/src/main/java/com/baeldung/properties/JsonProperties.java diff --git a/spring-boot/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java b/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java rename to spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java diff --git a/spring-boot/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java b/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java rename to spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java diff --git a/spring-boot/src/main/java/com/baeldung/properties/conversion/Employee.java b/spring-boot-properties/src/main/java/com/baeldung/properties/conversion/Employee.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/properties/conversion/Employee.java rename to spring-boot-properties/src/main/java/com/baeldung/properties/conversion/Employee.java diff --git a/spring-boot/src/main/java/com/baeldung/properties/conversion/EmployeeConverter.java b/spring-boot-properties/src/main/java/com/baeldung/properties/conversion/EmployeeConverter.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/properties/conversion/EmployeeConverter.java rename to spring-boot-properties/src/main/java/com/baeldung/properties/conversion/EmployeeConverter.java diff --git a/spring-boot/src/main/java/com/baeldung/properties/conversion/PropertiesConversionApplication.java b/spring-boot-properties/src/main/java/com/baeldung/properties/conversion/PropertiesConversionApplication.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/properties/conversion/PropertiesConversionApplication.java rename to spring-boot-properties/src/main/java/com/baeldung/properties/conversion/PropertiesConversionApplication.java diff --git a/spring-boot/src/main/java/com/baeldung/properties/conversion/PropertyConversion.java b/spring-boot-properties/src/main/java/com/baeldung/properties/conversion/PropertyConversion.java similarity index 100% rename from spring-boot/src/main/java/com/baeldung/properties/conversion/PropertyConversion.java rename to spring-boot-properties/src/main/java/com/baeldung/properties/conversion/PropertyConversion.java diff --git a/spring-core/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java b/spring-boot-properties/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java similarity index 100% rename from spring-core/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java rename to spring-boot-properties/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java diff --git a/spring-core/src/main/java/com/baeldung/value/InitializerBean.java b/spring-boot-properties/src/main/java/com/baeldung/value/InitializerBean.java similarity index 100% rename from spring-core/src/main/java/com/baeldung/value/InitializerBean.java rename to spring-boot-properties/src/main/java/com/baeldung/value/InitializerBean.java diff --git a/spring-core/src/main/java/com/baeldung/value/SomeBean.java b/spring-boot-properties/src/main/java/com/baeldung/value/SomeBean.java similarity index 100% rename from spring-core/src/main/java/com/baeldung/value/SomeBean.java rename to spring-boot-properties/src/main/java/com/baeldung/value/SomeBean.java diff --git a/spring-core/src/main/java/com/baeldung/value/ValuesApp.java b/spring-boot-properties/src/main/java/com/baeldung/value/ValuesApp.java similarity index 100% rename from spring-core/src/main/java/com/baeldung/value/ValuesApp.java rename to spring-boot-properties/src/main/java/com/baeldung/value/ValuesApp.java diff --git a/spring-core/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java b/spring-boot-properties/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java similarity index 100% rename from spring-core/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java rename to spring-boot-properties/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java diff --git a/spring-core/src/main/java/com/baeldung/yaml/MyApplication.java b/spring-boot-properties/src/main/java/com/baeldung/yaml/MyApplication.java similarity index 100% rename from spring-core/src/main/java/com/baeldung/yaml/MyApplication.java rename to spring-boot-properties/src/main/java/com/baeldung/yaml/MyApplication.java diff --git a/spring-core/src/main/java/com/baeldung/yaml/YAMLConfig.java b/spring-boot-properties/src/main/java/com/baeldung/yaml/YAMLConfig.java similarity index 100% rename from spring-core/src/main/java/com/baeldung/yaml/YAMLConfig.java rename to spring-boot-properties/src/main/java/com/baeldung/yaml/YAMLConfig.java diff --git a/spring-all/src/main/java/org/baeldung/properties/core/ComponentInXmlUsingProperties.java b/spring-boot-properties/src/main/java/org/baeldung/properties/core/ComponentInXmlUsingProperties.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/properties/core/ComponentInXmlUsingProperties.java rename to spring-boot-properties/src/main/java/org/baeldung/properties/core/ComponentInXmlUsingProperties.java diff --git a/spring-all/src/main/java/org/baeldung/properties/core/ComponentUsingProperties.java b/spring-boot-properties/src/main/java/org/baeldung/properties/core/ComponentUsingProperties.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/properties/core/ComponentUsingProperties.java rename to spring-boot-properties/src/main/java/org/baeldung/properties/core/ComponentUsingProperties.java diff --git a/spring-all/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithJavaConfig.java b/spring-boot-properties/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithJavaConfig.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithJavaConfig.java rename to spring-boot-properties/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithJavaConfig.java diff --git a/spring-all/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithXmlConfig.java b/spring-boot-properties/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithXmlConfig.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithXmlConfig.java rename to spring-boot-properties/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithXmlConfig.java diff --git a/spring-all/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithXmlConfigOne.java b/spring-boot-properties/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithXmlConfigOne.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithXmlConfigOne.java rename to spring-boot-properties/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithXmlConfigOne.java diff --git a/spring-all/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithXmlConfigTwo.java b/spring-boot-properties/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithXmlConfigTwo.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithXmlConfigTwo.java rename to spring-boot-properties/src/main/java/org/baeldung/properties/external/ExternalPropertiesWithXmlConfigTwo.java diff --git a/spring-all/src/main/java/org/baeldung/properties/spring/BasicPropertiesWithJavaConfig.java b/spring-boot-properties/src/main/java/org/baeldung/properties/spring/BasicPropertiesWithJavaConfig.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/properties/spring/BasicPropertiesWithJavaConfig.java rename to spring-boot-properties/src/main/java/org/baeldung/properties/spring/BasicPropertiesWithJavaConfig.java diff --git a/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfig.java b/spring-boot-properties/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfig.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfig.java rename to spring-boot-properties/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfig.java diff --git a/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfigOther.java b/spring-boot-properties/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfigOther.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfigOther.java rename to spring-boot-properties/src/main/java/org/baeldung/properties/spring/PropertiesWithJavaConfigOther.java diff --git a/spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithPlaceHolderConfigurer.java b/spring-boot-properties/src/main/java/org/baeldung/properties/spring/PropertiesWithPlaceHolderConfigurer.java similarity index 100% rename from spring-all/src/main/java/org/baeldung/properties/spring/PropertiesWithPlaceHolderConfigurer.java rename to spring-boot-properties/src/main/java/org/baeldung/properties/spring/PropertiesWithPlaceHolderConfigurer.java diff --git a/spring-core/src/main/resources/application.yml b/spring-boot-properties/src/main/resources/application.yml similarity index 100% rename from spring-core/src/main/resources/application.yml rename to spring-boot-properties/src/main/resources/application.yml diff --git a/spring-all/src/main/resources/bar.properties b/spring-boot-properties/src/main/resources/bar.properties similarity index 100% rename from spring-all/src/main/resources/bar.properties rename to spring-boot-properties/src/main/resources/bar.properties diff --git a/spring-all/src/main/resources/basicConfigForProperties.xml b/spring-boot-properties/src/main/resources/basicConfigForProperties.xml similarity index 100% rename from spring-all/src/main/resources/basicConfigForProperties.xml rename to spring-boot-properties/src/main/resources/basicConfigForProperties.xml diff --git a/spring-all/src/main/resources/basicConfigForPropertiesOne.xml b/spring-boot-properties/src/main/resources/basicConfigForPropertiesOne.xml similarity index 100% rename from spring-all/src/main/resources/basicConfigForPropertiesOne.xml rename to spring-boot-properties/src/main/resources/basicConfigForPropertiesOne.xml diff --git a/spring-all/src/main/resources/basicConfigForPropertiesTwo.xml b/spring-boot-properties/src/main/resources/basicConfigForPropertiesTwo.xml similarity index 100% rename from spring-all/src/main/resources/basicConfigForPropertiesTwo.xml rename to spring-boot-properties/src/main/resources/basicConfigForPropertiesTwo.xml diff --git a/spring-all/src/main/resources/child.properties b/spring-boot-properties/src/main/resources/child.properties similarity index 100% rename from spring-all/src/main/resources/child.properties rename to spring-boot-properties/src/main/resources/child.properties diff --git a/spring-all/src/main/resources/configForProperties.xml b/spring-boot-properties/src/main/resources/configForProperties.xml similarity index 100% rename from spring-all/src/main/resources/configForProperties.xml rename to spring-boot-properties/src/main/resources/configForProperties.xml diff --git a/spring-all/src/main/resources/configForPropertiesOne.xml b/spring-boot-properties/src/main/resources/configForPropertiesOne.xml similarity index 100% rename from spring-all/src/main/resources/configForPropertiesOne.xml rename to spring-boot-properties/src/main/resources/configForPropertiesOne.xml diff --git a/spring-boot/src/main/resources/configprops.json b/spring-boot-properties/src/main/resources/configprops.json similarity index 100% rename from spring-boot/src/main/resources/configprops.json rename to spring-boot-properties/src/main/resources/configprops.json diff --git a/spring-boot/src/main/resources/configprops.properties b/spring-boot-properties/src/main/resources/configprops.properties similarity index 89% rename from spring-boot/src/main/resources/configprops.properties rename to spring-boot-properties/src/main/resources/configprops.properties index 424b3632f9..71e81065ce 100644 --- a/spring-boot/src/main/resources/configprops.properties +++ b/spring-boot-properties/src/main/resources/configprops.properties @@ -22,3 +22,6 @@ item.name=Item name item.size=42 +#Additional properties +additional.unit=km +additional.max=100 \ No newline at end of file diff --git a/spring-boot-properties/src/main/resources/conversion.properties b/spring-boot-properties/src/main/resources/conversion.properties new file mode 100644 index 0000000000..94fadeb951 --- /dev/null +++ b/spring-boot-properties/src/main/resources/conversion.properties @@ -0,0 +1,2 @@ +conversion.timeInDefaultUnit=10 +conversion.timeInNano=9ns \ No newline at end of file diff --git a/spring-all/src/main/resources/foo.properties b/spring-boot-properties/src/main/resources/foo.properties similarity index 100% rename from spring-all/src/main/resources/foo.properties rename to spring-boot-properties/src/main/resources/foo.properties diff --git a/spring-all/src/main/resources/parent.properties b/spring-boot-properties/src/main/resources/parent.properties similarity index 100% rename from spring-all/src/main/resources/parent.properties rename to spring-boot-properties/src/main/resources/parent.properties diff --git a/spring-core/src/main/resources/values.properties b/spring-boot-properties/src/main/resources/values.properties similarity index 100% rename from spring-core/src/main/resources/values.properties rename to spring-boot-properties/src/main/resources/values.properties diff --git a/spring-core/src/main/resources/valueswithdefaults.properties b/spring-boot-properties/src/main/resources/valueswithdefaults.properties similarity index 100% rename from spring-core/src/main/resources/valueswithdefaults.properties rename to spring-boot-properties/src/main/resources/valueswithdefaults.properties diff --git a/spring-boot/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java b/spring-boot-properties/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java similarity index 93% rename from spring-boot/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java rename to spring-boot-properties/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java index 8f07b2da35..4777551132 100644 --- a/spring-boot/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java +++ b/spring-boot-properties/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java @@ -1,8 +1,5 @@ package com.baeldung.properties; -import org.baeldung.properties.AdditionalProperties; -import org.baeldung.properties.ConfigProperties; -import org.baeldung.properties.ConfigPropertiesDemoApplication; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java b/spring-boot-properties/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java similarity index 98% rename from spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java rename to spring-boot-properties/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java index e3d4c62953..2f0e5ae408 100644 --- a/spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java +++ b/spring-boot-properties/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java @@ -1,4 +1,4 @@ -package org.baeldung.properties; +package com.baeldung.properties; import java.util.Arrays; diff --git a/spring-boot/src/test/java/com/baeldung/properties/conversion/PropertiesConversionIntegrationTest.java b/spring-boot-properties/src/test/java/com/baeldung/properties/conversion/PropertiesConversionIntegrationTest.java similarity index 100% rename from spring-boot/src/test/java/com/baeldung/properties/conversion/PropertiesConversionIntegrationTest.java rename to spring-boot-properties/src/test/java/com/baeldung/properties/conversion/PropertiesConversionIntegrationTest.java diff --git a/spring-core/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java b/spring-boot-properties/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java similarity index 100% rename from spring-core/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java rename to spring-boot-properties/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java diff --git a/spring-all/src/test/java/org/baeldung/properties/basic/BasicPropertiesWithJavaIntegrationTest.java b/spring-boot-properties/src/test/java/org/baeldung/properties/basic/BasicPropertiesWithJavaIntegrationTest.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/basic/BasicPropertiesWithJavaIntegrationTest.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/basic/BasicPropertiesWithJavaIntegrationTest.java diff --git a/spring-all/src/test/java/org/baeldung/properties/basic/ExtendedPropertiesWithJavaIntegrationTest.java b/spring-boot-properties/src/test/java/org/baeldung/properties/basic/ExtendedPropertiesWithJavaIntegrationTest.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/basic/ExtendedPropertiesWithJavaIntegrationTest.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/basic/ExtendedPropertiesWithJavaIntegrationTest.java diff --git a/spring-all/src/test/java/org/baeldung/properties/basic/PropertiesWithJavaIntegrationTest.java b/spring-boot-properties/src/test/java/org/baeldung/properties/basic/PropertiesWithJavaIntegrationTest.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/basic/PropertiesWithJavaIntegrationTest.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/basic/PropertiesWithJavaIntegrationTest.java diff --git a/spring-all/src/test/java/org/baeldung/properties/basic/PropertiesWithMultipleXmlsIntegrationTest.java b/spring-boot-properties/src/test/java/org/baeldung/properties/basic/PropertiesWithMultipleXmlsIntegrationTest.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/basic/PropertiesWithMultipleXmlsIntegrationTest.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/basic/PropertiesWithMultipleXmlsIntegrationTest.java diff --git a/spring-all/src/test/java/org/baeldung/properties/basic/PropertiesWithXmlIntegrationTest.java b/spring-boot-properties/src/test/java/org/baeldung/properties/basic/PropertiesWithXmlIntegrationTest.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/basic/PropertiesWithXmlIntegrationTest.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/basic/PropertiesWithXmlIntegrationTest.java diff --git a/spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithJavaIntegrationTest.java b/spring-boot-properties/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithJavaIntegrationTest.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithJavaIntegrationTest.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithJavaIntegrationTest.java diff --git a/spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithMultipleXmlsIntegrationTest.java b/spring-boot-properties/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithMultipleXmlsIntegrationTest.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithMultipleXmlsIntegrationTest.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithMultipleXmlsIntegrationTest.java diff --git a/spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlManualTest.java b/spring-boot-properties/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlManualTest.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlManualTest.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlManualTest.java diff --git a/spring-all/src/test/java/org/baeldung/properties/multiple/MultiplePropertiesJavaConfigIntegrationTest.java b/spring-boot-properties/src/test/java/org/baeldung/properties/multiple/MultiplePropertiesJavaConfigIntegrationTest.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/multiple/MultiplePropertiesJavaConfigIntegrationTest.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/multiple/MultiplePropertiesJavaConfigIntegrationTest.java diff --git a/spring-all/src/test/java/org/baeldung/properties/multiple/MultiplePropertiesXmlConfigIntegrationTest.java b/spring-boot-properties/src/test/java/org/baeldung/properties/multiple/MultiplePropertiesXmlConfigIntegrationTest.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/multiple/MultiplePropertiesXmlConfigIntegrationTest.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/multiple/MultiplePropertiesXmlConfigIntegrationTest.java diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/ChildValueHolder.java b/spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/ChildValueHolder.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/parentchild/ChildValueHolder.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/ChildValueHolder.java diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertyPlaceHolderPropertiesIntegrationTest.java b/spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertyPlaceHolderPropertiesIntegrationTest.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertyPlaceHolderPropertiesIntegrationTest.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertyPlaceHolderPropertiesIntegrationTest.java diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertySourcePropertiesIntegrationTest.java b/spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertySourcePropertiesIntegrationTest.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertySourcePropertiesIntegrationTest.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/ParentChildPropertySourcePropertiesIntegrationTest.java diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/ParentValueHolder.java b/spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/ParentValueHolder.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/parentchild/ParentValueHolder.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/ParentValueHolder.java diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig.java b/spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig.java diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig2.java b/spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig2.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig2.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/config/ChildConfig2.java diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig.java b/spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig.java diff --git a/spring-all/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig2.java b/spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig2.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig2.java rename to spring-boot-properties/src/test/java/org/baeldung/properties/parentchild/config/ParentConfig2.java diff --git a/spring-all/src/test/java/org/baeldung/test/IntegrationTestSuite.java b/spring-boot-properties/src/test/java/org/baeldung/test/IntegrationTestSuite.java similarity index 100% rename from spring-all/src/test/java/org/baeldung/test/IntegrationTestSuite.java rename to spring-boot-properties/src/test/java/org/baeldung/test/IntegrationTestSuite.java diff --git a/spring-boot/src/test/resources/configprops-test.properties b/spring-boot-properties/src/test/resources/configprops-test.properties similarity index 100% rename from spring-boot/src/test/resources/configprops-test.properties rename to spring-boot-properties/src/test/resources/configprops-test.properties diff --git a/spring-boot-properties/src/test/resources/conversion.properties b/spring-boot-properties/src/test/resources/conversion.properties new file mode 100644 index 0000000000..640442ec33 --- /dev/null +++ b/spring-boot-properties/src/test/resources/conversion.properties @@ -0,0 +1,11 @@ +###### time unit +conversion.timeInDefaultUnit=10 +conversion.timeInNano=9ns +conversion.timeInDays=2 + +###### data size +conversion.sizeInDefaultUnit=300 +conversion.sizeInGB=2GB +conversion.sizeInTB=4 + +conversion.employee=john,2000 \ No newline at end of file diff --git a/spring-boot/README.MD b/spring-boot/README.MD index e159220cbc..04d76e438e 100644 --- a/spring-boot/README.MD +++ b/spring-boot/README.MD @@ -14,7 +14,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Custom Information in Spring Boot Info Endpoint](http://www.baeldung.com/spring-boot-info-actuator-custom) - [Using @JsonComponent in Spring Boot](http://www.baeldung.com/spring-boot-jsoncomponent) - [Testing in Spring Boot](http://www.baeldung.com/spring-boot-testing) -- [Guide to @ConfigurationProperties in Spring Boot](http://www.baeldung.com/configuration-properties-in-spring-boot) - [How to Get All Spring-Managed Beans?](http://www.baeldung.com/spring-show-all-beans) - [Spring Boot and Togglz Aspect](http://www.baeldung.com/spring-togglz) - [Getting Started with GraphQL and Spring Boot](http://www.baeldung.com/spring-graphql) @@ -29,9 +28,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Spring Shutdown Callbacks](http://www.baeldung.com/spring-shutdown-callbacks) - [Container Configuration in Spring Boot 2](http://www.baeldung.com/embeddedservletcontainercustomizer-configurableembeddedservletcontainer-spring-boot) - [Introduction to Chaos Monkey](https://www.baeldung.com/spring-boot-chaos-monkey) -- [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties) - [Display Auto-Configuration Report in Spring Boot](https://www.baeldung.com/spring-boot-auto-configuration-report) - [Injecting Git Information Into Spring](https://www.baeldung.com/spring-git-information) - [Validation in Spring Boot](https://www.baeldung.com/spring-boot-bean-validation) - [Guide to Creating and Running a Jar File in Java](https://www.baeldung.com/java-create-jar) -- [Guide to @EnableConfigurationProperties](https://www.baeldung.com/spring-enable-config-properties) diff --git a/spring-boot/src/main/java/org/baeldung/properties/ConfigProperties.java b/spring-boot/src/main/java/org/baeldung/properties/ConfigProperties.java deleted file mode 100644 index 1a3c985fe4..0000000000 --- a/spring-boot/src/main/java/org/baeldung/properties/ConfigProperties.java +++ /dev/null @@ -1,90 +0,0 @@ -package org.baeldung.properties; - -import java.util.List; -import java.util.Map; - -import javax.validation.constraints.Max; -import javax.validation.constraints.Min; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.Pattern; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.validation.annotation.Validated; - -@Configuration -@PropertySource("classpath:configprops.properties") -@ConfigurationProperties(prefix = "mail") -@Validated -public class ConfigProperties { - - @NotBlank - private String hostName; - - @Min(1025) - @Max(65536) - private int port; - - @Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$") - private String from; - - private List defaultRecipients; - private Map additionalHeaders; - private Credentials credentials; - - public String getHostName() { - return hostName; - } - - public void setHostName(String hostName) { - this.hostName = hostName; - } - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } - - public String getFrom() { - return from; - } - - public void setFrom(String from) { - this.from = from; - } - - public List getDefaultRecipients() { - return defaultRecipients; - } - - public void setDefaultRecipients(List defaultRecipients) { - this.defaultRecipients = defaultRecipients; - } - - public Map getAdditionalHeaders() { - return additionalHeaders; - } - - public void setAdditionalHeaders(Map additionalHeaders) { - this.additionalHeaders = additionalHeaders; - } - - public Credentials getCredentials() { - return credentials; - } - - public void setCredentials(Credentials credentials) { - this.credentials = credentials; - } - - @Bean - @ConfigurationProperties(prefix = "item") - public Item item(){ - return new Item(); - } -} diff --git a/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java b/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java deleted file mode 100644 index 7b8f2c3411..0000000000 --- a/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.baeldung.properties; - -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.context.annotation.ComponentScan; - -@SpringBootApplication -@ComponentScan(basePackageClasses = {ConfigProperties.class, - JsonProperties.class, - CustomJsonProperties.class, - AdditionalConfiguration.class}) -public class ConfigPropertiesDemoApplication { - public static void main(String[] args) { - new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer()) - .run(); - } - -} diff --git a/spring-boot/src/main/java/org/baeldung/properties/Credentials.java b/spring-boot/src/main/java/org/baeldung/properties/Credentials.java deleted file mode 100644 index 2d8ac76e62..0000000000 --- a/spring-boot/src/main/java/org/baeldung/properties/Credentials.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.baeldung.properties; - -import org.hibernate.validator.constraints.Length; -import org.springframework.validation.annotation.Validated; - -@Validated -public class Credentials { - - @Length(max = 4, min = 1) - private String authMethod; - private String username; - private String password; - - public String getAuthMethod() { - return authMethod; - } - - public void setAuthMethod(String authMethod) { - this.authMethod = authMethod; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } -} diff --git a/spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java b/spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java deleted file mode 100644 index 3fae8a8e98..0000000000 --- a/spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java +++ /dev/null @@ -1,71 +0,0 @@ -package org.baeldung.properties; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; - -@Component -@ConfigurationProperties(prefix = "custom") -public class CustomJsonProperties { - - private String host; - - private int port; - - private boolean resend; - - private Person sender; - - public static class Person { - - private String name; - private String address; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } - - public boolean isResend() { - return resend; - } - - public void setResend(boolean resend) { - this.resend = resend; - } - - public Person getSender() { - return sender; - } - - public void setSender(Person sender) { - this.sender = sender; - } -} diff --git a/spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java b/spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java deleted file mode 100644 index 5c31cd1344..0000000000 --- a/spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.baeldung.properties; - -import java.util.LinkedHashMap; -import java.util.List; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.PropertySource; -import org.springframework.stereotype.Component; - -@Component -@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class) -@ConfigurationProperties -public class JsonProperties { - - private String host; - - private int port; - - private boolean resend; - - private List topics; - - private LinkedHashMap sender; - - public LinkedHashMap getSender() { - return sender; - } - - public void setSender(LinkedHashMap sender) { - this.sender = sender; - } - - public List getTopics() { - return topics; - } - - public void setTopics(List topics) { - this.topics = topics; - } - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } - - public boolean isResend() { - return resend; - } - - public void setResend(boolean resend) { - this.resend = resend; - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } -} diff --git a/spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java deleted file mode 100644 index fd9b3f35a5..0000000000 --- a/spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.baeldung.properties; - -import java.io.IOException; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.core.env.MapPropertySource; -import org.springframework.core.env.PropertySource; -import org.springframework.core.io.Resource; - -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JsonPropertyContextInitializer implements ApplicationContextInitializer { - - private final static String CUSTOM_PREFIX = "custom."; - - @Override - @SuppressWarnings("unchecked") - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - try { - Resource resource = configurableApplicationContext.getResource("classpath:configprops.json"); - Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class); - Set set = readValue.entrySet(); - List propertySources = convertEntrySet(set, Optional.empty()); - for (PropertySource propertySource : propertySources) { - configurableApplicationContext.getEnvironment() - .getPropertySources() - .addFirst(propertySource); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - private static List convertEntrySet(Set entrySet, Optional parentKey) { - return entrySet.stream() - .map((Map.Entry e) -> convertToPropertySourceList(e, parentKey)) - .flatMap(Collection::stream) - .collect(Collectors.toList()); - } - - private static List convertToPropertySourceList(Map.Entry e, Optional parentKey) { - String key = parentKey.map(s -> s + ".") - .orElse("") + (String) e.getKey(); - Object value = e.getValue(); - return covertToPropertySourceList(key, value); - } - - @SuppressWarnings("unchecked") - private static List covertToPropertySourceList(String key, Object value) { - if (value instanceof LinkedHashMap) { - LinkedHashMap map = (LinkedHashMap) value; - Set entrySet = map.entrySet(); - return convertEntrySet(entrySet, Optional.ofNullable(key)); - } - String finalKey = CUSTOM_PREFIX + key; - return Collections.singletonList(new MapPropertySource(finalKey, Collections.singletonMap(finalKey, value))); - } - -} \ No newline at end of file diff --git a/spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java deleted file mode 100644 index 9578179519..0000000000 --- a/spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.baeldung.properties; - -import java.io.IOException; -import java.util.Map; - -import org.springframework.core.env.MapPropertySource; -import org.springframework.core.env.PropertySource; -import org.springframework.core.io.support.EncodedResource; -import org.springframework.core.io.support.PropertySourceFactory; - -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JsonPropertySourceFactory implements PropertySourceFactory { - - @Override - public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { - Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class); - return new MapPropertySource("json-property", readValue); - } - -} \ No newline at end of file diff --git a/spring-boot/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java deleted file mode 100644 index 48c551d1dd..0000000000 --- a/spring-boot/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.baeldung.properties; - -import java.util.Arrays; - -import org.baeldung.properties.ConfigPropertiesDemoApplication; -import org.baeldung.properties.CustomJsonProperties; -import org.baeldung.properties.JsonProperties; -import org.baeldung.properties.JsonPropertyContextInitializer; -import org.hamcrest.Matchers; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@ContextConfiguration(classes = ConfigPropertiesDemoApplication.class, initializers = JsonPropertyContextInitializer.class) -public class JsonPropertiesIntegrationTest { - - @Autowired - private JsonProperties jsonProperties; - - @Autowired - private CustomJsonProperties customJsonProperties; - - @Test - public void whenPropertiesLoadedViaJsonPropertySource_thenLoadFlatValues() { - Assert.assertEquals("mailer@mail.com", jsonProperties.getHost()); - Assert.assertEquals(9090, jsonProperties.getPort()); - Assert.assertTrue(jsonProperties.isResend()); - } - - @Test - public void whenPropertiesLoadedViaJsonPropertySource_thenLoadListValues() { - Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot"))); - } - - @Test - public void whenPropertiesLoadedViaJsonPropertySource_thenNestedLoadedAsMap() { - Assert.assertEquals("sender", jsonProperties.getSender() - .get("name")); - Assert.assertEquals("street", jsonProperties.getSender() - .get("address")); - } - - @Test - public void whenLoadedIntoEnvironment_thenFlatValuesPopulated() { - Assert.assertEquals("mailer@mail.com", customJsonProperties.getHost()); - Assert.assertEquals(9090, customJsonProperties.getPort()); - Assert.assertTrue(customJsonProperties.isResend()); - } - - @Test - public void whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() { - Assert.assertNotNull(customJsonProperties.getSender()); - Assert.assertEquals("sender", customJsonProperties.getSender() - .getName()); - Assert.assertEquals("street", customJsonProperties.getSender() - .getAddress()); - } - -} diff --git a/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java deleted file mode 100644 index f864fd4f8c..0000000000 --- a/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.baeldung.properties; - -import java.util.List; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = ConfigPropertiesDemoApplication.class) -@TestPropertySource("classpath:configprops-test.properties") -public class ConfigPropertiesIntegrationTest { - - @Autowired - private ConfigProperties properties; - - @Test - public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception { - Assert.assertEquals("Incorrectly bound hostName property", "host@mail.com", properties.getHostName()); - Assert.assertEquals("Incorrectly bound port property", 9000, properties.getPort()); - Assert.assertEquals("Incorrectly bound from property", "mailer@mail.com", properties.getFrom()); - } - - @Test - public void whenListPropertyQueriedthenReturnsProperty() throws Exception { - List defaultRecipients = properties.getDefaultRecipients(); - Assert.assertTrue("Couldn't bind list property!", defaultRecipients.size() == 2); - Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", defaultRecipients.size() == 2); - Assert.assertEquals("Incorrectly bound list[0] property", "admin@mail.com", defaultRecipients.get(0)); - Assert.assertEquals("Incorrectly bound list[1] property", "owner@mail.com", defaultRecipients.get(1)); - } - - @Test - public void whenMapPropertyQueriedthenReturnsProperty() throws Exception { - Map additionalHeaders = properties.getAdditionalHeaders(); - Assert.assertTrue("Couldn't bind map property!", additionalHeaders != null); - Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", additionalHeaders.size() == 3); - Assert.assertEquals("Incorrectly bound map[redelivery] property", "true", additionalHeaders.get("redelivery")); - Assert.assertEquals("Incorrectly bound map[secure] property", "true", additionalHeaders.get("secure")); - Assert.assertEquals("Incorrectly bound map[p3] property", "value", additionalHeaders.get("p3")); - } - - @Test - public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception { - Credentials credentials = properties.getCredentials(); - Assert.assertTrue("Couldn't bind map property!", credentials != null); - Assert.assertEquals("Incorrectly bound object property, authMethod", "SHA1", credentials.getAuthMethod()); - Assert.assertEquals("Incorrectly bound object property, username", "john", credentials.getUsername()); - Assert.assertEquals("Incorrectly bound object property, password", "password", credentials.getPassword()); - } - - @Test - public void whenBeanMethodAnnotatedThenPropertiesCorrectlyBound(){ - Item item = properties.item(); - Assert.assertEquals("Incorrectly bound object property, item.name","Test item name", item.getName()); - Assert.assertEquals("Incorrectly bound object property, item.size", 21, item.getSize()); - } -} diff --git a/spring-core/README.md b/spring-core/README.md index 18d14b7ecf..111b034bef 100644 --- a/spring-core/README.md +++ b/spring-core/README.md @@ -4,14 +4,10 @@ - [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean) - [Constructor Dependency Injection in Spring](http://www.baeldung.com/constructor-injection-in-spring) - [Constructor Injection in Spring with Lombok](http://www.baeldung.com/spring-injection-lombok) -- [A Quick Guide to Spring @Value](http://www.baeldung.com/spring-value-annotation) -- [Spring YAML Configuration](http://www.baeldung.com/spring-yaml) - [Introduction to Spring’s StreamUtils](http://www.baeldung.com/spring-stream-utils) -- [Using Spring @Value with Defaults](http://www.baeldung.com/spring-value-defaults) - [XML-Based Injection in Spring](http://www.baeldung.com/spring-xml-injection) - [A Quick Guide to the Spring @Lazy Annotation](http://www.baeldung.com/spring-lazy-annotation) - [Injecting Prototype Beans into a Singleton Instance in Spring](http://www.baeldung.com/spring-inject-prototype-bean-into-singleton) -- [How to Inject a Property Value Into a Class Not Managed by Spring?](http://www.baeldung.com/inject-properties-value-non-spring-class) - [@Lookup Annotation in Spring](http://www.baeldung.com/spring-lookup) - [BeanNameAware and BeanFactoryAware Interfaces in Spring](http://www.baeldung.com/spring-bean-name-factory-aware) - [Spring – Injecting Collections](http://www.baeldung.com/spring-injecting-collections)