Reading a List from properties file and load with spring annotation @Value

前端 未结 16 1363
陌清茗
陌清茗 2020-11-22 14:01

I want to have a list of values in a .properties file, ie:

my.list.of.strings=ABC,CDE,EFG

And to load it in my class directly, ie:

相关标签:
16条回答
  • 2020-11-22 14:19

    Using Spring EL:

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

    Assuming your properties file is loaded correctly with the following:

    my.list.of.strings=ABC,CDE,EFG
    
    0 讨论(0)
  • 2020-11-22 14:22

    By specifying the the my.list.of.strings=ABC,CDE,EFG in .properties file and using

    @Value("${my.list.of.strings}") private String[] myString;

    You can get the arrays of strings. And using CollectionUtils.addAll(myList, myString), you can get the list of strings.

    0 讨论(0)
  • 2020-11-22 14:22

    Beware of spaces in the values. I could be wrong, but I think spaces in the comma-separated list are not truncated using @Value and Spel. The list

    foobar=a, b, c
    

    would be read in as a list of strings

    "a", " b", " c"
    

    In most cases you would probably not want the spaces!

    The expression

    @Value("#{'${foobar}'.trim().replaceAll(\"\\s*(?=,)|(?<=,)\\s*\", \"\").split(',')}")
    private List<String> foobarList;
    

    would give you a list of strings:

    "a", "b", "c".
    

    The regular expression removes all spaces just before and just after a comma. Spaces inside of the values are not removed. So

    foobar = AA, B B, CCC
    

    should result in values

    "AA", "B B", "CCC".
    
    0 讨论(0)
  • 2020-11-22 14:23

    if using property placeholders then ser1702544 example would become

    @Value("#{myConfigProperties['myproperty'].trim().replaceAll(\"\\s*(?=,)|(?<=,)\\s*\", \"\").split(',')}") 
    

    With placeholder xml:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
        <property name="properties" ref="myConfigProperties" />
        <property name="placeholderPrefix"><value>$myConfigProperties{</value></property>
    </bean>    
    
    <bean id="myConfigProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
         <property name="locations">
             <list>
                    <value>classpath:myprops.properties</value>
             </list>
         </property>
    </bean> 
    
    0 讨论(0)
  • 2020-11-22 14:24

    If you are reading this and you are using Spring Boot, you have 1 more option for this feature

    Usually comma separated list are very clumsy for real world use case (And sometime not even feasible, if you want to use commas in your config):

    email.sendTo=somebody@example.com,somebody2@example.com,somebody3@example.com,.....
    

    With Spring Boot, you can write it like these (Index start at 0):

    email.sendTo[0]=somebody@example.com
    email.sendTo[1]=somebody2@example.com
    email.sendTo[2]=somebody3@example.com
    

    And use it like these:

    @Component
    @ConfigurationProperties("email")
    public class EmailProperties {
    
        private List<String> sendTo;
    
        public List<String> getSendTo() {
            return sendTo;
        }
    
        public void setSendTo(List<String> sendTo) {
            this.sendTo = sendTo;
        }
    
    }
    
    
    @Component
    public class EmailModel {
    
      @Autowired
      private EmailProperties emailProperties;
    
      //Use the sendTo List by 
      //emailProperties.getSendTo()
    
    }
    
    
    
    @Configuration
    public class YourConfiguration {
        @Bean
      public EmailProperties emailProperties(){
            return new EmailProperties();
      }
    
    }
    
    
    #Put this in src/main/resource/META-INF/spring.factories
      org.springframework.boot.autoconfigure.EnableAutoConfiguration=example.compackage.YourConfiguration
    
    0 讨论(0)
  • 2020-11-22 14:24

    My preferred way (for strings, in particular), is the following one:

    admin.user={'Doe, John','Headroom, Max','Mouse, Micky'}
    

    and use

    @Value("#{${admin.user}}")
    private List<String> userList;
    

    In this way, you can include also commas in your parameter. It works also for Sets.

    0 讨论(0)
提交回复
热议问题