diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java deleted file mode 100644 index 0329769d3c..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.value; - -public class ClassNotManagedBySpring { - - private String customVariable; - private String anotherCustomVariable; - - public ClassNotManagedBySpring(String someInitialValue, String anotherManagedValue) { - this.customVariable = someInitialValue; - this.anotherCustomVariable = anotherManagedValue; - } - - public String getCustomVariable() { - return customVariable; - } - - public void setCustomVariable(String customVariable) { - this.customVariable = customVariable; - } - - public String getAnotherCustomVariable() { - return anotherCustomVariable; - } - - public void setAnotherCustomVariable(String anotherCustomVariable) { - this.anotherCustomVariable = anotherCustomVariable; - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/CollectionProvider.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/CollectionProvider.java deleted file mode 100644 index fdc1f8ee03..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/CollectionProvider.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.value; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.PropertySource; -import org.springframework.stereotype.Component; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -@Component -@PropertySource("classpath:values.properties") -public class CollectionProvider { - - private final List values = new ArrayList<>(); - - public Collection getValues() { - return Collections.unmodifiableCollection(values); - } - - @Autowired - public void setValues(@Value("#{'${listOfValues}'.split(',')}") List values) { - this.values.addAll(values); - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/InitializerBean.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/InitializerBean.java deleted file mode 100644 index 8c8634c767..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/InitializerBean.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.value; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -@Component -public class InitializerBean { - - private String someInitialValue; - private String anotherManagedValue; - - public InitializerBean(@Value("someInitialValue") String someInitialValue, @Value("anotherValue") String anotherManagedValue) { - this.someInitialValue = someInitialValue; - this.anotherManagedValue = anotherManagedValue; - } - - public ClassNotManagedBySpring initClass() { - return new ClassNotManagedBySpring(this.someInitialValue, this.anotherManagedValue); - } - -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/PriorityProvider.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/PriorityProvider.java deleted file mode 100644 index 9d4b105afa..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/PriorityProvider.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.value; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.PropertySource; -import org.springframework.stereotype.Component; - -@Component -@PropertySource("classpath:values.properties") -public class PriorityProvider { - - private final String priority; - - @Autowired - public PriorityProvider(@Value("${priority:normal}") String priority) { - this.priority = priority; - } - - public String getPriority() { - return priority; - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/SomeBean.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/SomeBean.java deleted file mode 100644 index 39d5245049..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/SomeBean.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.value; - -public class SomeBean { - private int someValue; - - public SomeBean(int someValue) { - this.someValue = someValue; - } - - public int getSomeValue() { - return someValue; - } - - public void setSomeValue(int someValue) { - this.someValue = someValue; - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ValuesApp.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ValuesApp.java deleted file mode 100644 index 80893c1adf..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ValuesApp.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.baeldung.value; - -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -import javax.annotation.PostConstruct; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; - -@Configuration -@PropertySource(name = "myProperties", value = "values.properties") -public class ValuesApp { - - @Value("string value") - private String stringValue; - - @Value("${value.from.file}") - private String valueFromFile; - - @Value("${systemValue}") - private String systemValue; - - @Value("${unknown_param:some default}") - private String someDefault; - - @Value("${priority}") - private String prioritySystemProperty; - - @Value("${listOfValues}") - private String[] valuesArray; - - @Value("#{systemProperties['priority']}") - private String spelValue; - - @Value("#{systemProperties['unknown'] ?: 'some default'}") - private String spelSomeDefault; - - @Value("#{someBean.someValue}") - private Integer someBeanValue; - - @Value("#{'${listOfValues}'.split(',')}") - private List valuesList; - - @Value("#{${valuesMap}}") - private Map valuesMap; - - @Value("#{${valuesMap}.key1}") - private Integer valuesMapKey1; - - @Value("#{${valuesMap}['unknownKey']}") - private Integer unknownMapKey; - - @Value("#{${unknownMap : {key1:'1', key2 : '2'}}}") - private Map unknownMap; - - @Value("#{${valuesMap}['unknownKey'] ?: 5}") - private Integer unknownMapKeyWithDefaultValue; - - @Value("#{${valuesMap}.?[value>'1']}") - private Map valuesMapFiltered; - - @Value("#{systemProperties}") - private Map systemPropertiesMap; - - public static void main(String[] args) { - System.setProperty("systemValue", "Some system parameter value"); - System.setProperty("priority", "System property"); - ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesApp.class); - } - - @Bean - public SomeBean someBean() { - return new SomeBean(10); - } - - @PostConstruct - public void afterInitialize() { - System.out.println(stringValue); - System.out.println(valueFromFile); - System.out.println(systemValue); - System.out.println(someDefault); - System.out.println(prioritySystemProperty); - System.out.println(Arrays.toString(valuesArray)); - System.out.println(spelValue); - System.out.println(spelSomeDefault); - System.out.println(someBeanValue); - System.out.println(valuesList); - System.out.println(valuesMap); - System.out.println(valuesMapKey1); - System.out.println(unknownMapKey); - System.out.println(unknownMap); - System.out.println(unknownMapKeyWithDefaultValue); - System.out.println(valuesMapFiltered); - System.out.println(systemPropertiesMap); - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java deleted file mode 100644 index 589f891e6b..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.baeldung.valuewithdefaults; - -import java.util.Arrays; -import java.util.List; - -import javax.annotation.PostConstruct; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.util.Assert; - -import com.google.common.collect.Lists; -import com.google.common.primitives.Ints; - -/** - * Demonstrates setting defaults for @Value annotation. Note that there are no properties - * defined in the specified property source. We also assume that the user here - * does not have a system property named some.key. - * - */ -@Configuration -@PropertySource(name = "myProperties", value = "valueswithdefaults.properties") -public class ValuesWithDefaultsApp { - - @Value("${some.key:my default value}") - private String stringWithDefaultValue; - - @Value("${some.key:}") - private String stringWithBlankDefaultValue; - - @Value("${some.key:true}") - private boolean booleanWithDefaultValue; - - @Value("${some.key:42}") - private int intWithDefaultValue; - - @Value("${some.key:one,two,three}") - private String[] stringArrayWithDefaults; - - @Value("${some.key:1,2,3}") - private int[] intArrayWithDefaults; - - @Value("#{systemProperties['some.key'] ?: 'my default system property value'}") - private String spelWithDefaultValue; - - - public static void main(String[] args) { - ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesWithDefaultsApp.class); - } - - @PostConstruct - public void afterInitialize() { - // strings - Assert.isTrue(stringWithDefaultValue.equals("my default value")); - Assert.isTrue(stringWithBlankDefaultValue.equals("")); - - // other primitives - Assert.isTrue(booleanWithDefaultValue); - Assert.isTrue(intWithDefaultValue == 42); - - // arrays - List stringListValues = Lists.newArrayList("one", "two", "three"); - Assert.isTrue(Arrays.asList(stringArrayWithDefaults).containsAll(stringListValues)); - - List intListValues = Lists.newArrayList(1, 2, 3); - Assert.isTrue(Ints.asList(intArrayWithDefaults).containsAll(intListValues)); - - // SpEL - Assert.isTrue(spelWithDefaultValue.equals("my default system property value")); - - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/values.properties b/spring-boot-modules/spring-boot-properties/src/main/resources/values.properties deleted file mode 100644 index 9c85893d5f..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/resources/values.properties +++ /dev/null @@ -1,4 +0,0 @@ -value.from.file=Value got from the file -priority=high -listOfValues=A,B,C -valuesMap={key1:'1', key2 : '2', key3 : '3'} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/valueswithdefaults.properties b/spring-boot-modules/spring-boot-properties/src/main/resources/valueswithdefaults.properties deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java deleted file mode 100644 index 689801bece..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.value; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.context.junit4.SpringRunner; - -import static junit.framework.TestCase.assertEquals; -import static org.mockito.Mockito.when; - -@RunWith(SpringRunner.class) -public class ClassNotManagedBySpringIntegrationTest { - - @MockBean - private InitializerBean initializerBean; - - @Before - public void init() { - when(initializerBean.initClass()) - .thenReturn(new ClassNotManagedBySpring("This is only sample value", "Another configured value")); - } - - @Test - public void givenInitializerBean_whenInvokedInitClass_shouldInitialize() throws Exception { - - //given - ClassNotManagedBySpring classNotManagedBySpring = initializerBean.initClass(); - - //when - String initializedValue = classNotManagedBySpring.getCustomVariable(); - String anotherCustomVariable = classNotManagedBySpring.getAnotherCustomVariable(); - - //then - assertEquals("This is only sample value", initializedValue); - assertEquals("Another configured value", anotherCustomVariable); - - } - -} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/CollectionProviderIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/CollectionProviderIntegrationTest.java deleted file mode 100644 index 33552f2e1a..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/CollectionProviderIntegrationTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.value; - -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.junit4.SpringRunner; - -import static org.assertj.core.api.Assertions.assertThat; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = CollectionProvider.class) -public class CollectionProviderIntegrationTest { - - @Autowired - private CollectionProvider collectionProvider; - - @Test - public void givenPropertyFileWhenSetterInjectionUsedThenValueInjected() { - assertThat(collectionProvider.getValues()).contains("A", "B", "C"); - } -} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/PriorityProviderIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/PriorityProviderIntegrationTest.java deleted file mode 100644 index aa365a4b6a..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/PriorityProviderIntegrationTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.value; - -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.junit4.SpringRunner; - -import static org.assertj.core.api.Assertions.assertThat; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = PriorityProvider.class) -public class PriorityProviderIntegrationTest { - - @Autowired - private PriorityProvider priorityProvider; - - @Test - public void givenPropertyFileWhenConstructorInjectionUsedThenValueInjected() { - assertThat(priorityProvider.getPriority()).isEqualTo("high"); - } -} \ No newline at end of file