【问题标题】:Set int value from properties file using spring使用spring从属性文件中设置int值
【发布时间】: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 的情况下工作,不,你不能,因为你需要一个 PropertySourcesPlaceholderConfigurer bean 才能读取动态属性。
  • @ShiladittyaChakraborty 你试过用@Configuration注释包含connectionCloseTimeOut字段的类吗?
  • 是的。它是一个配置类,用配置注解
  • @ShiladittyaChakraborty 你能分享一下你有connectionCloseTimeOut字段的完整课程吗?
  • @MadhuBhat 添加配置类

标签: spring properties


【解决方案1】:

您的 SPEL 不正确。这应该工作

@Value("#{ T(java.lang.Integer).parseInt('${rabitmq.server.host.connectionclosetimeout}') }")
private int connectionCloseTimeOut; 

试试看

【讨论】:

  • 抛出异常:这一行有多个标记 - 字符常量无效
【解决方案2】:

以下来自PropertySourcehttps://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html的文档

为了使用 PropertySource 中的属性解析定义中的 ${...} 占位符或 @Value 注释,必须注册一个 PropertySourcesPlaceholderConfigurer。这在 XML 中使用时会自动发生,但在使用 @Configuration 类时必须使用静态 @Bean 方法显式注册。

因此,如果您使用@PropertySource,要解析@Value 注释中的${...} 占位符,您必须通过

注册PropertySourcesPlaceholderConfigurer
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

或通过如下方式在 XML 中配置 PSPC

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

请参考 spring JIRA 票 https://jira.spring.io/browse/SPR-8539 和 SO 线程 @Value not resolved when using @PropertySource annotation. How to configure PropertySourcesPlaceholderConfigurer? 以获得更多参考。

【讨论】:

  • @ShiladittyaChakraborty 您能否确认这是否回答了您的问题?
  • @ShiladittyaChakraborty 但您提到使用PropertySourcesPlaceholderConfigurer Bean 方法可以正常工作,对吧?如果您在配置类中使用该静态方法,则该属性应与 @Value 注释连接。
  • 它在我的开发机器上工作,但在 UAT 环境中不工作
  • @ShiladittyaChakraborty 您需要检查 Dev 和 UAT 环境之间的区别。这超出了您发布的问题的范围。如果它适用于您的开发,请接受答案。
  • @ShiladittyaChakraborty 如果这回答了你的问题,你能接受吗?
猜你喜欢
  • 2017-10-26
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 2021-09-19
相关资源
最近更新 更多