当前位置: 首页 > 知识库问答 >
问题:

使用spring从属性文件设置int值

商风华
2023-03-14

尝试使用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
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
@Value("#{new Integer.parseInt('${rabitmq.server.host.connectionclosetimeout}')}")
private int connectionCloseTimeOut; 

它也不起作用。

请建议什么是使它工作的方法。

共有1个答案

卢书
2023-03-14

你的说明不对。这应该行得通

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

试试看

 类似资料:
  • 例如,我有一个bean类 我想设置这个属性的值。 在Xml配置中,我可以 我如何实现同样的事情,即设置属性的值使用Java注释?现在我已经读到,我们可以使用@Value注释使用一些属性文件,但它不能不使用属性文件,做的方式,我通过xml文件?或者使用属性文件是必要的? 我可以通过包含

  • 问题内容: 配置 : Spring 2.5,Junit 4,Log4j 从系统属性中指定log4j文件位置 在运行时,使用-D java选项设置系统属性。一切都很好。 问题/我需要什么: 在单元测试时,未设置系统属性,并且文件位置未解析。 App使用Spring,想简单地配置Spring以 设置 系统属性。 更多信息: 要求仅用于配置。无法引入新的Java代码或IDE中的条目。理想情况下,Spri

  • 问题内容: 我在这里阅读了一些有关如何从Maven插件设置属性的问题(大多数问题涉及应用程序的版本号)。似乎没有简单的方法可以做到这一点,我发现最好的解决方案是拥有一个filter.properties文件,该文件从插件进行更新,并由主pom文件用来过滤所需的资源。 从Maven文档(Maven过滤器插件)中阅读以下内容后,我尝试了另一种解决方案: 变量可以包含在您的资源中。这些变量用$ {…}分

  • Section " SetFileAttributes" SetFileAttributes "C:\demo" HIDDEN ;设置文件夹属性为隐藏 SetFileAttributes "C:\demo\demo.txt" HIDDEN|READONLY ;设置文件属性为隐藏,只读 SetFileAttributes "C:\demo\demo.txt" NORMAL ;恢复

  • 我在任何地方都没见过这样的问题,所以我要在这里问一下。我必须为一个对象类型创建几个实例,我必须实例的数量应该从文件中提供。properties。我尝试使用注释@value,但它总是给我 NullPointerException 所以我不能解析它,因为它是空值。如何正确获取instances.number值? ...

  • 我刚刚使用一个xml配置文件将log4j转换为log4j2。一切正常,只是我似乎无法使用属性文件设置日志文件的路径。 这是一个Spring MVC应用程序,我有一个文件,位于文件夹中,以及log4j2.xml,i18n消息和其他属性文件。它有一个简单的条目:。我看过其他帖子,只是不知道如何正确配置log4j2。这是我所拥有的: 错误是: 替换实际路径有效,例如。, <代码> 我被捆绑语法的正确域部