I am struggling in obtaining both of the behaviors requested in the title. 1) I have a property file like this:
my.list=a,b,c
2) If that property is not present I want an empty list
Why the following is throwing me syntax error?
@Value("#{'${my.list}'.split(',') : T(java.util.Collections).emptyList()}")
Using Spring @Value with Defaults 1 Overview. Spring's @Value annotation provides a convenient way to inject property values into components. ... 2 String Defaults. If some.key cannot be resolved, then stringWithDefaultValue will be set to the default value of “ my default value”. 3 Primitives. ... 4 Arrays. ... 5 Using SpEL. ... 6 Conclusion. ...
Java: How to split a String into an ArrayList. Here are a few ways to split (or convert, or parse) a string of comma separated values: input = "aaa,bbb,ccc"; String[] arr = input.split(","); List<String> l = Arrays.asList(input.split(",")); List<String> l2 = new ArrayList<>(Arrays.asList(input.split(",")));
Our List contains a single element, which is equal to the value we set in our properties file. In order to properly inject a List, we need to use a special syntax called Spring Expression Language (SpEL):
Let's see how Spring behaves when we set our variable type to String []: We can see that Spring correctly assumes our delimiter is a comma and initializes the array accordingly. We should also note that, by default, injecting an array works correctly only when we have comma-separated values.
There is a way to get it working:
@Value("#{T(java.util.Arrays).asList('${my.list:}')}")
private List<String> list;
After the colon at my.list:
you can set the default value. For now its emtpy.
Did came across similar requirement. Below is one of the possible way of doing this :
@Value("#{'${some.key:}'.split(',')}")
Set<String> someKeySet;
I think similar should apply for List as well.
Pay attention to ":" after property name. It defaults to blank string which in turn would give empty list or set.
I don't think you can use nested SPEL. one way to achieve this is
@Value("${server.name:#{null}}")
private String someString;
private List<String> someList;
@PostConstruct
public void setList() {
someList = someString == null ? Collections.emptyList() : Arrays.asList(someString.split(","));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With