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
@@ -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);
}
}
@@ -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;
}
}