Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 使用@Value注释从属性文件读取整数数组_Spring_Properties_Annotations_Spring Annotations - Fatal编程技术网

Spring 使用@Value注释从属性文件读取整数数组

Spring 使用@Value注释从属性文件读取整数数组,spring,properties,annotations,spring-annotations,Spring,Properties,Annotations,Spring Annotations,我的属性文件中有以下内容: increments = 10,20,30 我想在我的POJO中阅读这些属性 @Value("#{'${increments}'.split(',')}") List<Integer> increments; List<Integer> getIncrements(){ return increments; } 然后在另一个类中使用此POJO List<Integer> increments = pojo.getIncre

我的属性文件中有以下内容:

increments = 10,20,30
我想在我的POJO中阅读这些属性

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

List<Integer> getIncrements(){
  return increments;
}
然后在另一个类中使用此POJO

List<Integer> increments = pojo.getIncrements();
然而,在最后一个调用中,我得到了一个转换错误-String到Integer。 为了克服这个问题,我将数组作为字符串数组读取,并在getIncrements方法中将其显式转换为整数数组


有没有一种方法可以在不进行显式类型转换的情况下,将属性文件中的整数数组读入POJO?

而不是只使用@Value${increments}的List int[]或Integer[]。Spring应该能够以这种方式为您进行类型转换。您使用的是哪个版本的Spring?我认为这些问题在一些小问题上得到了解决iteration@geoand我正在使用Spring3.1。0@ufdeveloper尝试升级到Spring 3.1.4。如果这不起作用,并且您有能力升级到Spring3.2,那么就选择Spring3.2.9,让我看看know@M.Deinum它适用于您的方法,但我仍然需要显式地将数组转换为列表。这比我刚才做的要干净得多。我将尝试geoand的建议,检查是否有可能在不进行显式转换的情况下执行此操作。