diff --git a/algorithms-sorting/src/main/java/com/baeldung/algorithms/inoutsort/InOutSort.java b/algorithms-sorting/src/main/java/com/baeldung/algorithms/inoutsort/InOutSort.java new file mode 100644 index 0000000000..5ba225cead --- /dev/null +++ b/algorithms-sorting/src/main/java/com/baeldung/algorithms/inoutsort/InOutSort.java @@ -0,0 +1,25 @@ +package com.baeldung.algorithms.inoutsort; + +public class InOutSort { + + public static int[] reverseInPlace(int A[]) { + int n = A.length; + for (int i = 0; i < n / 2; i++) { + int temp = A[i]; + A[i] = A[n - 1 - i]; + A[n - 1 - i] = temp; + } + + return A; + } + + public static int[] reverseOutOfPlace(int A[]) { + int n = A.length; + int[] B = new int[n]; + for (int i = 0; i < n; i++) { + B[n - i - 1] = A[i]; + } + + return B; + } +} diff --git a/algorithms-sorting/src/test/java/com/baeldung/algorithms/inoutsort/InOutSortUnitTest.java b/algorithms-sorting/src/test/java/com/baeldung/algorithms/inoutsort/InOutSortUnitTest.java new file mode 100644 index 0000000000..321b905f68 --- /dev/null +++ b/algorithms-sorting/src/test/java/com/baeldung/algorithms/inoutsort/InOutSortUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.algorithms.inoutsort; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertArrayEquals; + +import org.junit.Test; + +public class InOutSortUnitTest { + + @Test + public void givenArray_whenInPlaceSort_thenReversed() { + int[] input = {1, 2, 3, 4, 5, 6, 7}; + int[] expected = {7, 6, 5, 4, 3, 2, 1}; + assertArrayEquals("the two arrays are not equal", expected, InOutSort.reverseInPlace(input)); + } + + @Test + public void givenArray_whenOutOfPlaceSort_thenReversed() { + int[] input = {1, 2, 3, 4, 5, 6, 7}; + int[] expected = {7, 6, 5, 4, 3, 2, 1}; + assertArrayEquals("the two arrays are not equal", expected, InOutSort.reverseOutOfPlace(input)); + } +} diff --git a/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/incrementdecrementunaryoperators/IncrementDecrementUnaryOperatorUnitTest.java b/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/unaryoperators/IncrementDecrementUnaryOperatorUnitTest.java similarity index 97% rename from core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/incrementdecrementunaryoperators/IncrementDecrementUnaryOperatorUnitTest.java rename to core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/unaryoperators/IncrementDecrementUnaryOperatorUnitTest.java index 8fbc2eeab8..cb0f896ec0 100644 --- a/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/incrementdecrementunaryoperators/IncrementDecrementUnaryOperatorUnitTest.java +++ b/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/unaryoperators/IncrementDecrementUnaryOperatorUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.incrementdecrementunaryoperators; +package com.baeldung.unaryoperators; import static org.assertj.core.api.Assertions.assertThat; diff --git a/jackson-2/src/main/java/com/baeldung/jackson/entities/File.java b/jackson-2/src/main/java/com/baeldung/jackson/entities/File.java new file mode 100644 index 0000000000..0e8829e927 --- /dev/null +++ b/jackson-2/src/main/java/com/baeldung/jackson/entities/File.java @@ -0,0 +1,25 @@ +package com.baeldung.jackson.entities; + +public class File { + + private Long id; + + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} \ No newline at end of file diff --git a/jackson-2/src/main/java/com/baeldung/jackson/entities/Folder.java b/jackson-2/src/main/java/com/baeldung/jackson/entities/Folder.java new file mode 100644 index 0000000000..c49e46b204 --- /dev/null +++ b/jackson-2/src/main/java/com/baeldung/jackson/entities/Folder.java @@ -0,0 +1,82 @@ +package com.baeldung.jackson.entities; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +public class Folder { + + private Long id; + + private String name; + + private String owner; + + private Date created; + + private Date modified; + + private Date lastAccess; + + @JsonIgnore + private List files = new ArrayList<>(); + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public Date getCreated() { + return created; + } + + public void setCreated(Date created) { + this.created = created; + } + + public Date getModified() { + return modified; + } + + public void setModified(Date modified) { + this.modified = modified; + } + + public Date getLastAccess() { + return lastAccess; + } + + public void setLastAccess(Date lastAccess) { + this.lastAccess = lastAccess; + } + + public List getFiles() { + return files; + } + + public void setFiles(List files) { + this.files = files; + } + +} \ No newline at end of file diff --git a/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderBeanSerializerModifier.java b/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderBeanSerializerModifier.java new file mode 100644 index 0000000000..a3add58dc4 --- /dev/null +++ b/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderBeanSerializerModifier.java @@ -0,0 +1,21 @@ +package com.baeldung.jackson.serialization.custom.serializer; + +import com.baeldung.jackson.entities.Folder; +import com.fasterxml.jackson.databind.BeanDescription; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializationConfig; +import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; + +public class FolderBeanSerializerModifier extends BeanSerializerModifier { + + @Override + public JsonSerializer modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer serializer) { + + if (beanDesc.getBeanClass().equals(Folder.class)) { + return new FolderSerializerWithDefaultSerializerStored((JsonSerializer) serializer); + } + + return serializer; + } + +} diff --git a/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializer.java b/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializer.java new file mode 100644 index 0000000000..f385e63e09 --- /dev/null +++ b/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializer.java @@ -0,0 +1,36 @@ +package com.baeldung.jackson.serialization.custom.serializer; + +import java.io.IOException; + +import com.baeldung.jackson.entities.File; +import com.baeldung.jackson.entities.Folder; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class FolderSerializer extends StdSerializer { + + public FolderSerializer() { + super(Folder.class); + } + + @Override + public void serialize(Folder value, JsonGenerator gen, SerializerProvider provider) throws IOException { + + gen.writeStartObject(); + gen.writeStringField("name", value.getName()); + + gen.writeArrayFieldStart("files"); + for (File file : value.getFiles()) { + gen.writeStartObject(); + gen.writeNumberField("id", file.getId()); + gen.writeStringField("name", file.getName()); + gen.writeEndObject(); + } + gen.writeEndArray(); + + gen.writeEndObject(); + + } + +} diff --git a/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializerWithCallingOwnSerializer.java b/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializerWithCallingOwnSerializer.java new file mode 100644 index 0000000000..ed5d9fffb8 --- /dev/null +++ b/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializerWithCallingOwnSerializer.java @@ -0,0 +1,30 @@ +package com.baeldung.jackson.serialization.custom.serializer; + +import java.io.IOException; + +import com.baeldung.jackson.entities.Folder; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class FolderSerializerWithCallingOwnSerializer extends StdSerializer { + + public FolderSerializerWithCallingOwnSerializer() { + super(Folder.class); + } + + @Override + public void serialize(Folder value, JsonGenerator gen, SerializerProvider provider) throws IOException { + + gen.writeStartObject(); + gen.writeStringField("name", value.getName()); + + provider.defaultSerializeField("files", value.getFiles(), gen); + + provider.defaultSerializeField("details", value, gen); + + gen.writeEndObject(); + + } + +} diff --git a/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializerWithDefaultSerializerStored.java b/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializerWithDefaultSerializerStored.java new file mode 100644 index 0000000000..d4a95cd939 --- /dev/null +++ b/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializerWithDefaultSerializerStored.java @@ -0,0 +1,35 @@ +package com.baeldung.jackson.serialization.custom.serializer; + +import java.io.IOException; + +import com.baeldung.jackson.entities.Folder; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class FolderSerializerWithDefaultSerializerStored extends StdSerializer { + + private final JsonSerializer defaultSerializer; + + public FolderSerializerWithDefaultSerializerStored(JsonSerializer defaultSerializer) { + super(Folder.class); + this.defaultSerializer = defaultSerializer; + } + + @Override + public void serialize(Folder value, JsonGenerator gen, SerializerProvider provider) throws IOException { + + gen.writeStartObject(); + gen.writeStringField("name", value.getName()); + + provider.defaultSerializeField("files", value.getFiles(), gen); + + gen.writeFieldName("details"); + defaultSerializer.serialize(value, gen, provider); + + gen.writeEndObject(); + + } + +} diff --git a/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializerWithInternalObjectMapper.java b/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializerWithInternalObjectMapper.java new file mode 100644 index 0000000000..b23dc32205 --- /dev/null +++ b/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializerWithInternalObjectMapper.java @@ -0,0 +1,34 @@ +package com.baeldung.jackson.serialization.custom.serializer; + +import java.io.IOException; + +import com.baeldung.jackson.entities.Folder; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class FolderSerializerWithInternalObjectMapper extends StdSerializer { + + public FolderSerializerWithInternalObjectMapper() { + super(Folder.class); + } + + @Override + public void serialize(Folder value, JsonGenerator gen, SerializerProvider provider) throws IOException { + + gen.writeStartObject(); + gen.writeStringField("name", value.getName()); + + // we access internal mapper to delegate the serialization of File list + ObjectMapper mapper = (ObjectMapper) gen.getCodec(); + + gen.writeFieldName("files"); + String stringValue = mapper.writeValueAsString(value.getFiles()); + gen.writeRawValue(stringValue); + + gen.writeEndObject(); + + } + +} diff --git a/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializerWithSerializerProvider.java b/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializerWithSerializerProvider.java new file mode 100644 index 0000000000..81990a084d --- /dev/null +++ b/jackson-2/src/main/java/com/baeldung/jackson/serialization/custom/serializer/FolderSerializerWithSerializerProvider.java @@ -0,0 +1,29 @@ +package com.baeldung.jackson.serialization.custom.serializer; + +import java.io.IOException; + +import com.baeldung.jackson.entities.Folder; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; + +public class FolderSerializerWithSerializerProvider extends StdSerializer { + + public FolderSerializerWithSerializerProvider() { + super(Folder.class); + } + + @Override + public void serialize(Folder value, JsonGenerator gen, SerializerProvider provider) throws IOException { + + gen.writeStartObject(); + gen.writeStringField("name", value.getName()); + + // we delegate the File list serialization to its default serializer + provider.defaultSerializeField("files", value.getFiles(), gen); + + gen.writeEndObject(); + + } + +} \ No newline at end of file diff --git a/jackson-2/src/test/java/com/baeldung/jackson/serialization/custom/serializer/CallingDefaultSerializerUnitTest.java b/jackson-2/src/test/java/com/baeldung/jackson/serialization/custom/serializer/CallingDefaultSerializerUnitTest.java new file mode 100644 index 0000000000..b067e7b501 --- /dev/null +++ b/jackson-2/src/test/java/com/baeldung/jackson/serialization/custom/serializer/CallingDefaultSerializerUnitTest.java @@ -0,0 +1,166 @@ +package com.baeldung.jackson.serialization.custom.serializer; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.jackson.entities.File; +import com.baeldung.jackson.entities.Folder; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; + +public class CallingDefaultSerializerUnitTest { + + private ObjectMapper mapper; + private Folder mockFolder; + private TypeReference> mapType; + + @Before + public void setup() { + + mapType = new TypeReference>() { + }; + + mapper = new ObjectMapper(); + + mockFolder = new Folder(); + mockFolder.setId(1L); + mockFolder.setName("Root Folder"); + mockFolder.setOwner("root"); + mockFolder.setCreated(Date.from(Instant.now().minusSeconds(60))); + mockFolder.setModified(Date.from(Instant.now().minusSeconds(30))); + mockFolder.setLastAccess(Date.from(Instant.now())); + + File file1 = new File(); + file1.setId(1L); + file1.setName("File 1"); + + File file2 = new File(); + file2.setId(2L); + file2.setName("File 2"); + + List files = new ArrayList<>(); + files.add(file1); + files.add(file2); + mockFolder.setFiles(files); + + } + + @Test + public void givenFolder_whenSerialized_onlyNameAndFilesFieldsSerialized() throws IOException { + + SimpleModule module = new SimpleModule(); + module.addSerializer(new FolderSerializer()); + mapper.registerModule(module); + + String json = mapper.writeValueAsString(mockFolder); + + HashMap actual = mapper.readValue(json, mapType); + + assertTrue(actual.containsKey("name")); + assertTrue(actual.containsKey("files")); + assertEquals(mockFolder.getName(), actual.get("name")); + + List actualFiles = (List) actual.get("files"); + assertEquals(mockFolder.getFiles().size(), actualFiles.size()); + + } + + @Test + public void givenFolder_whenSerializedWithSerializerProvider_onlyNameAndFilesFieldsSerialized() throws IOException { + + SimpleModule module = new SimpleModule(); + module.addSerializer(new FolderSerializerWithSerializerProvider()); + mapper.registerModule(module); + + String json = mapper.writeValueAsString(mockFolder); + + HashMap actual = mapper.readValue(json, mapType); + + assertTrue(actual.containsKey("name")); + assertTrue(actual.containsKey("files")); + assertEquals(mockFolder.getName(), actual.get("name")); + + List actualFiles = (List) actual.get("files"); + assertEquals(mockFolder.getFiles().size(), actualFiles.size()); + + } + + @Test + public void givenFolder_whenSerializedWithInternalObjectMapper_onlyNameAndFilesFieldsSerialized() throws IOException { + + SimpleModule module = new SimpleModule(); + module.addSerializer(new FolderSerializerWithInternalObjectMapper()); + mapper.registerModule(module); + + String json = mapper.writeValueAsString(mockFolder); + + HashMap actual = mapper.readValue(json, mapType); + + assertTrue(actual.containsKey("name")); + assertTrue(actual.containsKey("files")); + assertEquals(mockFolder.getName(), actual.get("name")); + + List actualFiles = (List) actual.get("files"); + assertEquals(mockFolder.getFiles().size(), actualFiles.size()); + + } + + @Test(expected = StackOverflowError.class) + public void givenFolder_whenSerializedWithCallingOwnSerializer_exceptionOccured() throws IOException { + + SimpleModule module = new SimpleModule(); + module.addSerializer(new FolderSerializerWithCallingOwnSerializer()); + mapper.registerModule(module); + + mapper.writeValueAsString(mockFolder); + + } + + @Test + public void givenFolder_whenSerializedWithDefaultSerializerStored_NameAndFilesAndDetailsFieldsSerialized() throws IOException { + + SimpleModule module = new SimpleModule(); + module.setSerializerModifier(new FolderBeanSerializerModifier()); + mapper.registerModule(module); + + String json = mapper.writeValueAsString(mockFolder); + + HashMap actual = mapper.readValue(json, mapType); + + assertTrue(actual.containsKey("name")); + assertTrue(actual.containsKey("files")); + assertEquals(mockFolder.getName(), actual.get("name")); + + List actualFiles = (List) actual.get("files"); + assertEquals(mockFolder.getFiles().size(), actualFiles.size()); + + Map actualDetails = (Map) actual.get("details"); + assertTrue(actualDetails.containsKey("id")); + assertTrue(actualDetails.containsKey("name")); + assertTrue(actualDetails.containsKey("owner")); + assertTrue(actualDetails.containsKey("created")); + assertTrue(actualDetails.containsKey("modified")); + assertTrue(actualDetails.containsKey("lastAccess")); + + assertEquals(mockFolder.getId().longValue(), ((Number)actualDetails.get("id")).longValue()); + assertEquals(mockFolder.getName(), actualDetails.get("name")); + assertEquals(mockFolder.getOwner(), actualDetails.get("owner")); + assertEquals(mockFolder.getCreated(), new Date((long) actualDetails.get("created"))); + assertEquals(mockFolder.getModified(), new Date((long) actualDetails.get("modified"))); + assertEquals(mockFolder.getLastAccess(), new Date((long) actualDetails.get("lastAccess"))); + + } + +} diff --git a/java-math/src/main/java/com/baeldung/algorithms/gcd/GCDImplementation.java b/java-math/src/main/java/com/baeldung/algorithms/gcd/GCDImplementation.java new file mode 100644 index 0000000000..d4844abd9c --- /dev/null +++ b/java-math/src/main/java/com/baeldung/algorithms/gcd/GCDImplementation.java @@ -0,0 +1,55 @@ +package com.baeldung.algorithms.gcd; + +public class GCDImplementation { + + public static int gcdByBruteForce(int n1, int n2) { + int gcd = 1; + for (int i = 1; i <= n1 && i <= n2; i++) { + if (n1 % i == 0 && n2 % i == 0) { + gcd = i; + } + } + return gcd; + } + + public static int gcdByEuclidsAlgorithm(int n1, int n2) { + if (n2 == 0) { + return n1; + } + return gcdByEuclidsAlgorithm(n2, n1 % n2); + } + + public static int gcdBySteinsAlgorithm(int n1, int n2) { + if (n1 == 0) { + return n2; + } + + if (n2 == 0) { + return n1; + } + + int n; + for (n = 0; ((n1 | n2) & 1) == 0; n++) { + n1 >>= 1; + n2 >>= 1; + } + + while ((n1 & 1) == 0) { + n1 >>= 1; + } + + do { + while ((n2 & 1) == 0) { + n2 >>= 1; + } + + if (n1 > n2) { + int temp = n1; + n1 = n2; + n2 = temp; + } + n2 = (n2 - n1); + } while (n2 != 0); + return n1 << n; + } +} diff --git a/java-math/src/test/java/com/baeldung/algorithms/gcd/GCDImplementationUnitTest.java b/java-math/src/test/java/com/baeldung/algorithms/gcd/GCDImplementationUnitTest.java new file mode 100644 index 0000000000..d2c91a2eb8 --- /dev/null +++ b/java-math/src/test/java/com/baeldung/algorithms/gcd/GCDImplementationUnitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.algorithms.gcd; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class GCDImplementationUnitTest { + + @Test + public void whenCalculatingGCDByBruteForceMethod_thenCorrect() { + int n1 = 60; + int n2 = 90; + int gcd = GCDImplementation.gcdByBruteForce(n1, n2); + assertThat(gcd).isEqualTo(30); + } + + @Test + public void whenCalculatingGCDByEuclidsAlgorithm_thenCorrect() { + int n1 = 60; + int n2 = 90; + int gcd = GCDImplementation.gcdByEuclidsAlgorithm(n1, n2); + assertThat(gcd).isEqualTo(30); + } + + @Test + public void whenCalculatingGCDBySteinsAlgorithm_thenCorrect() { + int n1 = 60; + int n2 = 90; + int gcd = GCDImplementation.gcdBySteinsAlgorithm(n1, n2); + assertThat(gcd).isEqualTo(30); + } +} diff --git a/parent-boot-1/pom.xml b/parent-boot-1/pom.xml index 1054038623..8f1af24466 100644 --- a/parent-boot-1/pom.xml +++ b/parent-boot-1/pom.xml @@ -55,7 +55,7 @@ 3.1.0 - 1.5.19.RELEASE + 1.5.22.RELEASE diff --git a/parent-boot-2/pom.xml b/parent-boot-2/pom.xml index f0e921bf37..edc115d186 100644 --- a/parent-boot-2/pom.xml +++ b/parent-boot-2/pom.xml @@ -78,6 +78,6 @@ 3.3.0 1.0.22.RELEASE - 2.1.6.RELEASE + 2.1.7.RELEASE diff --git a/patterns/backoff-jitter/README.md b/patterns/backoff-jitter/README.md index 6459e4c8e0..a3940cc418 100644 --- a/patterns/backoff-jitter/README.md +++ b/patterns/backoff-jitter/README.md @@ -1,2 +1,2 @@ ### Relevant Articles: -- [Better Retries with Exponential Backoff and Jitter](https://baeldung.com/retries-with-exponential-backoff-and-jitter) +- [Better Retries with Exponential Backoff and Jitter](https://www.baeldung.com/resilience4j-backoff-jitter) diff --git a/persistence-modules/java-sql2o/README.md b/persistence-modules/java-sql2o/README.md index a86e45f1c8..d3586dc9ed 100644 --- a/persistence-modules/java-sql2o/README.md +++ b/persistence-modules/java-sql2o/README.md @@ -1,3 +1,3 @@ ### Relevant Articles: -- [A Guide to Sql2o](http://www.baeldung.com/sql2o) +- [A Guide to Sql2o](https://www.baeldung.com/java-sql2o) 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-data/src/test/java/com/baeldung/disableautoconfig/SpringDataJPAIntegrationTest.java b/spring-boot-data/src/test/java/com/baeldung/disableautoconfig/SpringDataJPAIntegrationTest.java index a465979b3c..ef07df7824 100644 --- a/spring-boot-data/src/test/java/com/baeldung/disableautoconfig/SpringDataJPAIntegrationTest.java +++ b/spring-boot-data/src/test/java/com/baeldung/disableautoconfig/SpringDataJPAIntegrationTest.java @@ -19,7 +19,7 @@ public class SpringDataJPAIntegrationTest { private ApplicationContext context; @Test(expected = NoSuchBeanDefinitionException.class) - public void givenAutoconfigurationIsDisable_whenApplicationStarts_thenContextWillNotHaveTheAutoconfiguredClasses() { + public void givenAutoConfigDisabled_whenStarting_thenNoAutoconfiguredBeansInContext() { context.getBean(DataSource.class); } diff --git a/spring-boot-data/src/test/java/com/baeldung/disableautoconfig/SpringDataMongoDBIntegrationTest.java b/spring-boot-data/src/test/java/com/baeldung/disableautoconfig/SpringDataMongoDBIntegrationTest.java index bdfadf76ce..cc66f71aa2 100644 --- a/spring-boot-data/src/test/java/com/baeldung/disableautoconfig/SpringDataMongoDBIntegrationTest.java +++ b/spring-boot-data/src/test/java/com/baeldung/disableautoconfig/SpringDataMongoDBIntegrationTest.java @@ -18,7 +18,7 @@ public class SpringDataMongoDBIntegrationTest { private ApplicationContext context; @Test(expected = NoSuchBeanDefinitionException.class) - public void givenAutoconfigurationIsDisable_whenApplicationStarts_thenContextWillNotHaveTheAutoconfiguredClasses() { + public void givenAutoConfigDisabled_whenStarting_thenNoAutoconfiguredBeansInContext() { context.getBean(MongoTemplate.class); } diff --git a/spring-boot-data/src/test/java/com/baeldung/disableautoconfig/SpringDataRedisIntegrationTest.java b/spring-boot-data/src/test/java/com/baeldung/disableautoconfig/SpringDataRedisIntegrationTest.java index 10133cace3..657e624db3 100644 --- a/spring-boot-data/src/test/java/com/baeldung/disableautoconfig/SpringDataRedisIntegrationTest.java +++ b/spring-boot-data/src/test/java/com/baeldung/disableautoconfig/SpringDataRedisIntegrationTest.java @@ -18,7 +18,7 @@ public class SpringDataRedisIntegrationTest { private ApplicationContext context; @Test(expected = NoSuchBeanDefinitionException.class) - public void givenAutoconfigurationIsDisable_whenApplicationStarts_thenContextWillNotHaveTheAutoconfiguredClasses() { + public void givenAutoConfigDisabled_whenStarting_thenNoAutoconfiguredBeansInContext() { context.getBean(RedisTemplate.class); } 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)