Spring .properties file: get element as an Array

后端 未结 5 1738
灰色年华
灰色年华 2020-12-02 15:21

I\'m loading properties attributes from a .properties file using Spring as follows:

file: elements.properties
base.module.elementToSearch=1
base         


        
相关标签:
5条回答
  • 2020-12-02 15:33

    And incase you a different delimiter other than comma, you can use that as well.

    @Value("#{'${my.config.values}'.split(',')}")
    private String[] myValues;   // could also be a List<String>
    

    and

    in your application properties you could have

    my.config.values=value1, value2, value3
    
    0 讨论(0)
  • 2020-12-02 15:36

    If you need to pass the asterisk symbol, you have to wrap it with quotes.

    In my case, I need to configure cors for websockets. So, I decided to put cors urls into application.yml. For prod env I'll use specific urls, but for dev it's ok to use just *.

    In yml file I have:

    websocket:
      cors: "*"
    

    In Config class I have:

    @Value("${websocket.cors}")
    private String[] cors;
    
    0 讨论(0)
  • 2020-12-02 15:48

    If you define your array in properties file like:

    base.module.elementToSearch=1,2,3,4,5,6
    

    You can load such array in your Java class like this:

      @Value("${base.module.elementToSearch}")
      private String[] elementToSearch;
    
    0 讨论(0)
  • 2020-12-02 15:49

    With a Spring Boot one can do the following:

    application.properties

    values[0]=abc
    values[1]=def
    

    Configuration class

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Component
    @ConfigurationProperties
    public class Configuration {
    
        List<String> values = new ArrayList<>();
    
        public List<String> getValues() {
            return values;
        }
    
    }
    

    This is needed, without this class or without the values in class it is not working.

    Spring Boot Application class

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import java.util.List;
    
    @SpringBootApplication
    public class SpringBootConsoleApplication implements CommandLineRunner {
    
        private static Logger LOG = LoggerFactory.getLogger(SpringBootConsoleApplication.class);
    
        // notice #{} is used instead of ${}
        @Value("#{configuration.values}")
        List<String> values;
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootConsoleApplication.class, args);
        }
    
        @Override
        public void run(String... args) {
            LOG.info("values: {}", values);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-02 15:50

    Here is an example of how you can do it in Spring 4.0+

    application.properties content:

    some.key=yes,no,cancel
    

    Java Code:

    @Autowire
    private Environment env;
    
    ...
    
    String[] springRocks = env.getProperty("some.key", String[].class);
    
    0 讨论(0)
提交回复
热议问题