75

我正在尝试将值的输出转换为整数:

@Value("${api.orders.pingFrequency}")
private Integer pingFrequency;

以上抛出错误

org.springframework.beans.TypeMismatchException: 
    Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; 
nested exception is java.lang.NumberFormatException: 
    For input string: "(java.lang.Integer)${api.orders.pingFrequency}"

我也试过@Value("(java.lang.Integer)${api.orders.pingFrequency}")

谷歌似乎没有在这个问题上说太多。我想总是处理一个整数,而不是在使用它的任何地方解析这个值。

解决方法

我意识到一种解决方法可能是使用 setter 方法为我运行转换,但如果 Spring 可以做到,我宁愿学习一些关于 Spring 的知识。

4

10 回答 10

57

假设您的类路径中有一个属性文件,其中包含

api.orders.pingFrequency=4

我试过里面@Controller

@Controller
public class MyController {     
    @Value("${api.orders.pingFrequency}")
    private Integer pingFrequency;
    ...
}

我的 servlet 上下文包含:

<context:property-placeholder location="classpath:myprops.properties" />

它工作得很好。

因此,要么您的属性不是整数类型,要么您没有正确配置属性占位符,或者您使用了错误的属性键。

我尝试使用无效的属性值运行,4123;. 我得到的例外是

java.lang.NumberFormatException: For input string: "4123;"

这让我觉得你的财产的价值是

api.orders.pingFrequency=(java.lang.Integer)${api.orders.pingFrequency}
于 2013-03-13T19:38:10.187 回答
32

我在互联网上寻找答案,我发现了以下内容

@Value("#{new java.text.SimpleDateFormat('${aDateFormat}').parse('${aDateStr}')}")
Date myDate;

所以在你的情况下你可以试试这个

@Value("#{new Integer('${api.orders.pingFrequency}')}")
private Integer pingFrequency;
于 2013-03-13T19:35:28.540 回答
7

如果要将属性从属性文件转换为整数,我发现有2 个解决方案:

给定场景:customer.properties包含customer.id = 100作为字段,您希望在 spring 配置文件中以整数形式访问它。属性customerId在 Bean Customer中声明为int类型

解决方案1:

<property name="customerId" value="#{T(java.lang.Integer).parseInt('${customer.id}')}" />

在上面的行中,属性文件中的字符串值被转换为int类型。

解决方案2:使用其他扩展名代替propeties。例如-如果您的属性文件名为 customer.properties ,则将其设为customer.details并在配置文件中使用以下代码

<property name="customerId"  	value="${customer.id}" />

于 2014-12-07T10:37:57.473 回答
7

我有完全相同的情况。这是由于 Spring 上下文中没有PropertySourcesPlaceholderConfigurer@Value引起的,它根据类内部的注释解析值。

包含一个属性占位符来解决问题,整数不需要使用Spring表达式(如果使用,属性文件不必存在ignore-resource-not-found="true"):

<context:property-placeholder location="/path/to/my/app.properties"
    ignore-resource-not-found="true" />
于 2015-05-19T14:09:19.273 回答
6

如果您使用的是 @Configuation,则在静态 bean 下实例化。如果不是静态的 @Configutation 很早就被实例化,并且负责解析 @Value、@Autowired 等注释的 BeanPostProcessors 无法对其采取行动。参考这里

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
   return new PropertySourcesPlaceholderConfigurer();
}
于 2017-03-23T20:33:08.413 回答
1

我用这个解决了同样的问题。请参阅此Spring MVC:@Value 注释以获取在 *.properties 文件中定义的 int 值

@Value(#{propertyfileId.propertyName})

作品

于 2016-02-29T12:19:57.577 回答
0

当您有 2 个具有相同文件名的资源时也会出现此问题;在类路径中配置的 2 个不同的 jar 或目录路径中说“configurations.properties”。例如:

您的流程或 Web 应用程序(jar、war 或 ear)中有“configurations.properties”。但是另一个依赖项(jar)在同一路径中具有相同的文件“configurations.properties”。然后我想 Spring 不知道(@_@?)从哪里获取属性,只是发送在 @Value 注释中声明的属性名称。

于 2016-10-03T13:25:37.637 回答
0

就我而言,问题在于我的 POST 请求被发送到与 GET 相同的 url(使用“?..=..”获取参数),并且该参数与表单参数具有相同的名称。可能 Spring 正在将它们合并到一个数组中并且解析抛出错误。

于 2016-12-18T14:14:49.650 回答
0

使用@Value 时,应在Class 上添加@PropertySource 注解,或者在spring 的xml 文件中指定properties holder。例如。

@Component
@PropertySource("classpath:config.properties")
public class BusinessClass{
   @Value("${user.name}")
   private String name;
   @Value("${user.age}")
   private int age;
   @Value("${user.registed:false}")
   private boolean registed;
}

配置属性

user.name=test
user.age=20
user.registed=true

这行得通!

当然,您可以使用占位符 xml 配置来代替注解。春天.xml

<context:property-placeholder location="classpath:config.properties"/>
于 2018-03-30T03:00:45.730 回答
0

由于使用@Value("new Long("myconfig")")和 cast 可能会在启动时抛出错误,如果找不到配置或者不是相同的预期数字格式

我们使用了以下方法,并且通过故障安全检查按预期工作。

@Configuration()
public class MyConfiguration {

   Long DEFAULT_MAX_IDLE_TIMEOUT = 5l;

   @Value("db.timeoutInString")
   private String timeout;

   public Long getTimout() {
        final Long timoutVal = StringUtil.parseLong(timeout);
        if (null == timoutVal) {
            return DEFAULT_MAX_IDLE_TIMEOUT;
        }
        return timoutVal;
    }
}
   
于 2020-07-13T08:12:16.040 回答