263

I am trying to load an array of strings from the application.yml file. This is the config:

ignore:
    filenames:
        - .DS_Store
        - .hg

This is the class fragment:

@Value("${ignore.filenames}")
private List<String> igonoredFileNames = new ArrayList<>();

There are other configurations in the same class that loads just fine. There are no tabs in my YAML file. Still, I get the following exception:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'ignore.filenames' in string value "${ignore.filenames}"

11 Answers 11

221

use comma separated values in application.yml

ignoreFilenames: .DS_Store, .hg

java code for access

@Value("${ignoreFilenames}")    
String[] ignoreFilenames

It is working ;)

4
  • I was hoping this would work in Dropwizard too :'( thanks though!
    – RST
    Commented May 16, 2018 at 18:33
  • 10
    In my own tests, this is equivalent to ".DS_Store, .hg" but not ".DS_Store", ".hg" - the latter fails with "org.yaml.snakeyaml.parser.ParserException: while parsing a block mapping". So what you're actually doing here is providing the property as a single comma-separated string (which Spring then splits into an array or list) instead of a YAML array. This "works" but doesn't answer the original question. I have yet to find a way to parse a YAML array in a @Value annotated bean property.
    – Sonafets
    Commented Mar 13, 2019 at 12:42
  • 1
    List<String> rather than String[] works as well
    – Prectron
    Commented Dec 9, 2022 at 20:06
  • Create separate property config class and use List<String> instead of String[]. And in yml file configure like ignore: filenames: - .DS_Store - .hg Commented Jul 14, 2023 at 12:00
121

My guess is, that the @Value can not cope with "complex" types. You can go with a prop class like this:

@Component
@ConfigurationProperties('ignore')
class IgnoreSettings {
    List<String> filenames
}

Please note: This code is Groovy - not Java - to keep the example short! See the comments for tips how to adopt.

See the complete example https://github.com/christoph-frick/so-springboot-yaml-string-list

9
  • 4
    You can bind a comma-separated with @Value (as long as a converter is registered, which it will be in Spring Boot I think).
    – Dave Syer
    Commented Nov 3, 2014 at 8:29
  • @Bahadır i tried this code here and it works. @ComponentScan and friends picks this up?
    – cfrick
    Commented Nov 4, 2014 at 22:45
  • 2
    It is picking up string settings but not List<String> settings Commented Nov 5, 2014 at 12:05
  • 3
    There must also be a getter defined for the property.
    – OrangeDog
    Commented Feb 10, 2016 at 17:56
  • 2
    This looks like it is a limitation with the spring boot processes that annotation - github.com/spring-projects/spring-boot/issues/501
    – wontondon
    Commented Sep 19, 2016 at 15:39
72

From the Spring Boot docs:

YAML lists are represented as property keys with [index] dereferencers, for example this YAML:

my:
   servers:
       - dev.bar.com
       - foo.bar.com

Would be transformed into these properties:

my.servers[0]=dev.bar.com
my.servers[1]=foo.bar.com

To bind to properties like that using the Spring DataBinder utilities (which is what @ConfigurationProperties does) you need to have a property in the target bean of type java.util.List and you either need to provide a setter, or initialize it with a mutable value, e.g. this will bind to the properties above. Here is what the question's code would look like.

(Note: The example below only works on Spring Boot 3.0 and greater. Previous versions require the @ConstructorBinding annotation to be present to not throw exceptions when this object is being constructed.)

@ConfigurationProperties(prefix="ignore")
//@ConstructorBinding // uncomment if using Spring Boot 2.x.
public class Filenames {

    private List<String> ignoredFilenames = new ArrayList<String>();

    public List<String> getFilenames() {
        return this.ignoredFilenames;
    }
}
4
  • 4
    This should be working, BTW getXxx() is necessary for it to work, and must use a List, not Set.
    – Eric
    Commented Nov 28, 2017 at 3:15
  • In the yaml, the list of values for the ArrayList can be a comma delimited list. In my case, I have many values, so a compact list is cleaner than an item per line. So in the example, you can do servers: dev.bar.com, foo.bar.com
    – Mr. Lee
    Commented Jul 29, 2019 at 21:08
  • 1
    Could the yaml be loaded from a properties file like this: servers: ${my.servers}? To get my.servers[0] and my.servers[1] into the yaml? I'm trying to have stage dependent configurations
    – ochs.tobi
    Commented Jul 28, 2020 at 7:58
  • Combination of ConfigurationProperties and Component is faced with error: Annotated with ConstructorBinding but defined as Spring component Commented May 19 at 14:51
57

In addition to Ahmet's answer you can add line breaks to the coma separated string using > symbol.

application.yml:

ignoreFilenames: >
  .DS_Store, 
  .hg

Java code:

@Value("${ignoreFilenames}")    
String[] ignoreFilenames;
2
  • This works well as long as you don't have any more commas (,) in your strings. Commented Jan 8, 2020 at 12:58
  • 1
    This doesn't work to read properties as arraylist. It reads them as single String.
    – ssbh
    Commented May 15, 2020 at 20:12
50

In my case, this was a syntax issue in the .yml file. I had:

@Value("${spring.kafka.bootstrap-servers}")
public List<String> BOOTSTRAP_SERVERS_LIST;

and the list in my .yml file:

bootstrap-servers:
    - s1.company.com:9092
    - s2.company.com:9092
    - s3.company.com:9092

was not reading into the @Value-annotated field. When I changed the syntax in the .yml file to:

bootstrap-servers: >
    s1.company.com:9092,
    s2.company.com:9092,
    s3.company.com:9092

it worked fine.

4
  • 3
    Trying this on spring-boot 1.5 this does not work. You need a colon after bootstrap-servers Commented Jul 10, 2019 at 22:16
  • Using spring boot 2.x, the list has a single element of all the values with space separated. Commented Sep 25, 2022 at 10:44
  • 3
    @Taugenichts You're correct a colon is missing. bootstrap-servers: >
    – Gagan
    Commented Oct 20, 2022 at 12:59
  • I wouldn't call it a "syntax issue in the YAML file" – the syntax in the YAML is correct for a list of strings, just @Value can't cope with complex types. Commented Mar 22 at 10:44
19

Well, the only thing I can make it work is like so:

servers: >
    dev.example.com,
    another.example.com

@Value("${servers}")
private String[] array;

And dont forget the @Configuration above your class....

Without the "," separation, no such luck...

Works too (boot 1.5.8 versie)

servers: 
       dev.example.com,
       another.example.com
1
  • I prefer the first version with > though. If you put comment (e.g. after every line) IDE (IntelliJ) won't highlight the comment it in the first case but highlights it in the second case. It seems like it is supported to put the comment in the second case but it is not. The parser fails in both cases if the comment is there (spring-boot 2.2.10). Commented May 6, 2021 at 20:01
14
@Value("#{'${your.elements}'.split(',')}")  
private Set<String> stringSet;

yml file:

your:
 elements: element1, element2, element3

There is lot more you can play with spring spEL.

1
  • 1
    Unnecessary because Spring already does this split by comma by default! Commented Jun 21, 2023 at 19:26
11

Ahmet's answer provides on how to assign the comma separated values to String array.

To use the above configuration in different classes you might need to create getters/setters for this.. But if you would like to load this configuration once and keep using this as a bean with Autowired annotation, here is the how I accomplished:

In ConfigProvider.java

@Bean (name = "ignoreFileNames")
@ConfigurationProperties ( prefix = "ignore.filenames" )
public List<String> ignoreFileNames(){
    return new ArrayList<String>();
}

In outside classes:

@Autowired
@Qualifier("ignoreFileNames")
private List<String> ignoreFileNames;

you can use the same list everywhere else by autowiring.

1
  • 1
    That works. In my case though the application failed, unless I add @Configuration at the beginning of the class in ConfigProvider.java. Otherwise i got Source required a bean of type 'java.util.List' that could not be found.
    – chujudzvin
    Commented Jun 24, 2019 at 12:27
3

Configuration in yaml file:

ignore:
    filenames: >
        .DS_Store
        .hg

In spring component:

@Value("#{'${gnore.filenames}'.split(' ')}")
private List<String> igonoredFileNames;

This worked fine for me.

0
0

@Value("${coupon.update.fieldsToIgnore}") private List fieldsToIgnore;

coupon:
  update:
    fieldsToIgnore: dbVersion, createdAt, updatedAt

simply use this

-3
@Value("${your.elements}")    
private String[] elements;

yml file:

your:
 elements: element1, element2, element3
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.