Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.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
Java spring@Value int自动剖分式浮点_Java_Spring_Spring Mvc - Fatal编程技术网

Java spring@Value int自动剖分式浮点

Java spring@Value int自动剖分式浮点,java,spring,spring-mvc,Java,Spring,Spring Mvc,当我在属性文件中有值并以类似的方式读取它们时 @Value(${ftpserver.maxconnections}) private float maxConnections; 它可以工作,如果值为0f,spring会自动解析该值。 然而,整数没有f,l,d,但我需要一个int。 如果我只写没有后缀的数字,Spring会抱怨它无法将java.lang.String解析为int。 根据各种互联网来源,它应该有效,但它没有 我将文件设置为 @PropertySource("classpath:ap

当我在属性文件中有值并以类似的方式读取它们时

@Value(${ftpserver.maxconnections})
private float maxConnections;
它可以工作,如果值为0f,spring会自动解析该值。 然而,整数没有f,l,d,但我需要一个int。 如果我只写没有后缀的数字,Spring会抱怨它无法将java.lang.String解析为int。 根据各种互联网来源,它应该有效,但它没有

我将文件设置为

@PropertySource("classpath:application.properties")
在同一配置文件中

@Bean
public static PropertySourcesPlaceholderConfigurer pspc() {
   return new PropertySourcesPlaceholderConfigurer();
}
总而言之,我希望这能奏效

@Value(${ftpserver.maxconnections})
private float maxConnections;

无法从Spring ContextLoader中将字符串解析为int,因此无法获取异常。

您需要定义一个
转换服务:

@Bean 
public ConversionService conversionService() {
    return new DefaultConversionService();
}

您不能在与使用此@Value的类相同的类中定义此
conversionService()
(因为转换器不会初始化)

您可以使用以下方法将属性文件中的对象解析为int:
@Value(“#{T(java.lang.Integer).parseInt('${property.name:{number}'))}”)