Spring RefreshScope with SpEL deferred Evaluation

Viewed 79

I'm having trouble knowing how this is possible or a potential work around. I am looking at configuring variables that are in the form of "foo,bar,baz" into a List as separate elements. Currently the code looks like

@RefreshScope
@Configuration
@Getter
public class fakeConfiguration {
    @Value("#{PropertySplitter.toSet('${properties:}')}")
    private final Set<String> field = new HashSet<>();
}   

@Component("PropertySplitter")
@SuppressWarnings("unused")
public class PropertySplitter {
    public Set<String> toSet(String property) {
        Set<String> set = new HashSet<>();

        if(!property.trim().isEmpty()){
            Collections.addAll(set, property.split("\\s*,\\s*"));
        }

        return set;
    }
}

This properly evaluates the String list into a Set however the refreshScope never gets triggered properly. If I use

    @Value("${properties:}")
    private final String fieldAsString;

I can see the field properly refresh, but I'd like to actively convert the value to a list as it is changed. Is this possible?

1 Answers
  1. In newer version of spring-boot below works for application.properties and Application.yml
    @Value("#{${props.list}}")
    private List<String> fieldList;
  1. If you use Application.yml you can arrange the objects
    props:
      list:
        - val1
        - val2

and then use in code

    @Value("${props.list}")
    private List<String> ymlList

Last, You can try the below as well

    @Value("#{'${props.list}'.split(',')}") 
    private List<String> myList;
Related