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}'))}”)