Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-11759

@Value annotation should be able to inject List<String> from YAML properties

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Waiting for Triage
    • Component/s: Core
    • Labels:
      None

      Description

      Yaml file

       
      foobar:  
        ignoredUserIds:
          - 57016311
          - 22588218

      Class

       
          
      public class Foobar {
          @Value("${foobar.ignoredUserIds}")
          List<String> ignoredUserIds;
      }
       

      Error

       
      Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: java.util.List foobar.Foobar.ignoredUserIds; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'foobar.ignoredUserIds' in string value "${foobar.ignoredUserIds}"
       

        Activity

        Hide
        snicoll Stéphane Nicoll added a comment -

        Yeah well, your code there reproduces the limitation described in this issue. And this issue is not fixed so what you're experiencing is pretty much the current state yeah.

        You are using Spring Boot so please do you a favour and stop using @Value. @ConfigurationProperties does a lot more and does support that use case.

        Show
        snicoll Stéphane Nicoll added a comment - Yeah well, your code there reproduces the limitation described in this issue. And this issue is not fixed so what you're experiencing is pretty much the current state yeah. You are using Spring Boot so please do you a favour and stop using @Value . @ConfigurationProperties does a lot more and does support that use case.
        Hide
        wenjiezhang2013@gmail.com Wenjie Zhang added a comment - - edited

        I encountered this problem when I was using spring framework to load yaml configuration:

        @Bean
        public static PropertySourcesPlaceholderConfigurer properties() {
            final PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
            final YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            yaml.setResources(new ClassPathResource("application.yml"));
            propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
            return propertySourcesPlaceholderConfigurer;
        }
         
        //The property is 
        @Value("${platforms}")
        private List<String> platforms;
        

        I can see the properties get loaded into the PropertySources object like platforms[0], platforms[1].

        However, I still get following exception:

        Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'platforms' in string value "${platforms}"
        	at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
        	at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
        

        Show
        wenjiezhang2013@gmail.com Wenjie Zhang added a comment - - edited I encountered this problem when I was using spring framework to load yaml configuration: @Bean public static PropertySourcesPlaceholderConfigurer properties() { final PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); final YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean(); yaml.setResources( new ClassPathResource( "application.yml" )); propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject()); return propertySourcesPlaceholderConfigurer; }   //The property is @Value ( "${platforms}" ) private List<String> platforms; I can see the properties get loaded into the PropertySources object like platforms [0] , platforms [1] . However, I still get following exception: Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'platforms' in string value "${platforms}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java: 174 ) at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java: 126 )
        Hide
        wenjiezhang2013@gmail.com Wenjie Zhang added a comment -

        Do you guys think making the @Value to support regular expression is a good idea to resolve this bug?

        Show
        wenjiezhang2013@gmail.com Wenjie Zhang added a comment - Do you guys think making the @Value to support regular expression is a good idea to resolve this bug?
        Hide
        romero.rsp Romero Ricardo added a comment -

        Yeah, 2017 and I'm still facing that same issue, the bug is not fixed. Do you guys think you can assign this issue to me so I get it sorted?

        Show
        romero.rsp Romero Ricardo added a comment - Yeah, 2017 and I'm still facing that same issue, the bug is not fixed. Do you guys think you can assign this issue to me so I get it sorted?
        Hide
        snicoll Stéphane Nicoll added a comment -

        Feel free to give that a try Romero, no need to be assigned for that.

        Show
        snicoll Stéphane Nicoll added a comment - Feel free to give that a try Romero, no need to be assigned for that.

          People

          • Assignee:
            juergen.hoeller Juergen Hoeller
            Reporter:
            aberlin@pivotallabs.com Adam Berlin
            Last updater:
            Stéphane Nicoll
          • Votes:
            18 Vote for this issue
            Watchers:
            18 Start watching this issue

            Dates

            • Created:
              Updated:
              Days since last comment:
              5 weeks, 4 days ago