BAEL-4069: Add example of using @Value with constructor/setter (#9318)
This commit is contained in:
+27
@@ -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);
|
||||
}
|
||||
}
|
||||
+22
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user