简体   繁体   English

从 yaml 文件中读取整数列表

[英]Read Integer List from yaml file

The current implementation is :-当前的实现是:-

public static final List<Integer> validCodes = Collections.unmodifiableList(Arrays.asList(110, 210, 310,510,610));

However, I am not happy with this approach as this of hard coded.但是,我对这种硬编码的方法并不满意。 I want to make it configurable instead of hard-coded.我想让它可配置而不是硬编码。 I suppose reading this values from the yaml file would solve my problem.我想从 yaml 文件中读取这些值可以解决我的问题。

But how do I define a list of Integer in the yaml file and real it using @value.但是我如何在 yaml 文件中定义一个整数列表并使用@value 来实现它。 I can find a lot of example about reading list of strings but not integers.我可以找到很多关于读取字符串列表但不是整数的示例。

Within the application.yml file, define the config variable as follows.application.yml文件中,如下定义配置变量。

my_integers: 1231,12323,12321

then use the @Value annotation to make to an Integer list as follows.然后使用@Value注释生成一个整数列表,如下所示。

@Value("#{'${my_integers}'.split(',')}")
private List<Integer> myIntegers;

Spring does the rest.剩下的就是春天了。 You only need to use myIntegers with your need.您只需要根据需要使用myIntegers Happy coding....快乐编码....

PS: I have used spring boot 2.0.* PS:我用过spring boot 2.0.*

One way:单程:

Let's say if the properties file contains假设属性文件是否包含

intArr={1,2,3}

Then @Value can be used like:然后@Value可以像这样使用:

@Value("#{${intArr}}")
Integer[] intArr;

Second way:第二种方式:

If the property contains comma separated values as: intArr: [1, 2, 3]如果属性包含逗号分隔值,如: intArr: [1, 2, 3]

Then the annotation code would be:然后注释代码将是:

@Value("${intArr}")
private int[] intArr;

Edit:编辑:

You can configure ConversionServiceFactoryBean which activates the new configuration service which supports converting String to Collection types.您可以配置ConversionServiceFactoryBean ,它激活支持将 String 转换为 Collection 类型的新配置服务。

By activating this, it will support following kind of conversion:通过激活它,它将支持以下类型的转换:

 intArray= 1, 2, 3, 4

And the following code:以及以下代码:

@Value("${intArray}")
private List<Integer> myList;

Ref here参考这里

You can used following method for getting all available properties from YML file.您可以使用以下方法从 YML 文件中获取所有可用属性。 When you used following method, you have to add following jar into your build script.当您使用以下方法时,您必须将以下 jar 添加到您的构建脚本中。

compile group: 'org.yaml', name: 'snakeyaml', version: '1.12'   

I think this will help you to continue your task.我认为这将帮助你继续你的任务。

private Map<String, Object> loadYMLData() {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            String fileName = "{{YAMAL FILE NAME}}.yml";
            Yaml yaml = new Yaml();
            ClassLoader classLoader = getClass().getClassLoader();
            File file = new File(classLoader.getResource(fileName).getFile());
            InputStream ios = new FileInputStream(file);

            // Parse the YAML file and return the output as a series of Maps and Lists
            result = (Map<String, Object>) yaml.load(ios);
            System.out.println(result.toString());

        } catch (Exception e) {
            logger.error("Error==>", e);
        }
        return result;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM