【发布时间】:2018-07-26 13:34:18
【问题描述】:
尝试使用 spring 绑定属性文件中的 int 值
但每次都低于异常:
Failed to instantiate [org.springframework.amqp.rabbit.connection.ConnectionFactory]:
Factory method 'connectionFactory' threw exception; nested exception is java.lang.NumberFormatException: For input string: \"${rabitmq.server.host.connectionclosetimeout}\"
Caused by: java.lang.NumberFormatException: For input string: \"${rabitmq.server.host.connectionclosetimeout}\""}}
我的属性文件如下所示:
rabitmq.server.host.connectionclosetimeout=30000
我的豆子
@Value("${rabitmq.server.host.connectionclosetimeout}")
private int connectionCloseTimeOut;
配置类
@Configuration
@PropertySource("classpath:config/service.properties")
public class RabbitMqConfiguration {
@Value("${rabitmq.server.host.connectionclosetimeout}")
private Integer connectionCloseTimeOut;
/**
* Establishing connection
*
* @return
*/
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
connectionFactory.setCloseTimeout(connectionCloseTimeOut);
return connectionFactory;
}
}
如果我在下面添加 bean,那么它工作正常。但是我想在没有bean的情况下工作
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
我也尝试过以下方法:
@Value("#{new Integer.parseInt('${rabitmq.server.host.connectionclosetimeout}')}")
private int connectionCloseTimeOut;
它也不起作用。
请建议什么是让它工作的方法。
【问题讨论】:
-
如果我在 bean 下面添加,那么它工作正常。但是我想在没有下面 bean 的情况下工作,不,你不能,因为你需要一个
PropertySourcesPlaceholderConfigurerbean 才能读取动态属性。 -
@ShiladittyaChakraborty 你试过用@Configuration注释包含connectionCloseTimeOut字段的类吗?
-
是的。它是一个配置类,用配置注解
-
@ShiladittyaChakraborty 你能分享一下你有
connectionCloseTimeOut字段的完整课程吗? -
@MadhuBhat 添加配置类
标签: spring properties