BAEL-4069: Add example of using @Value with constructor/setter (#9318)

This commit is contained in:
kwoyke 2020-05-19 21:59:55 +02:00 committed by GitHub
parent 73762873f6
commit 7bc07fdac9
5 changed files with 94 additions and 1 deletions

View File

@ -0,0 +1,27 @@
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<String> values = new ArrayList<>();
public Collection<String> getValues() {
return Collections.unmodifiableCollection(values);
}
@Autowired
public void setValues(@Value("#{'${listOfValues}'.split(',')}") List<String> values) {
this.values.addAll(values);
}
}

View File

@ -0,0 +1,22 @@
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;
}
}

View File

@ -1,4 +1,4 @@
value.from.file=Value got from the file
priority=Properties file
priority=high
listOfValues=A,B,C
valuesMap={key1:'1', key2 : '2', key3 : '3'}

View File

@ -0,0 +1,22 @@
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");
}
}

View File

@ -0,0 +1,22 @@
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("Properties file");
}
}