1

Trying to bind int value from properties file using spring

But everytime getting below exception :

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}\""}}

My properties file look like below :

rabitmq.server.host.connectionclosetimeout=30000

My Bean

@Value("${rabitmq.server.host.connectionclosetimeout}")
private int connectionCloseTimeOut; 

Configuration Class

@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;
    }       

}

If I add below bean then its working fine. But I want to work without below bean

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

I have also tried with below method :

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

It's also not working.

Please suggest what is way to get it working.

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94

2 Answers2

0

Your SPEL isn't correct. This should work

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

Try it out

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
pvpkiran
  • 25,582
  • 8
  • 87
  • 134
0

Below is from the doc of PropertySource https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html

In order to resolve ${...} placeholders in definitions or @Value annotations using properties from a PropertySource, one must register a PropertySourcesPlaceholderConfigurer. This happens automatically when using in XML, but must be explicitly registered using a static @Bean method when using @Configuration classes.

So if you are using @PropertySource, to resolve the ${...} placeholders in @Value annotations you must register the PropertySourcesPlaceholderConfigurer either by

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

or by configuring the PSPC in the XML as below

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

Please refer the spring JIRA ticket https://jira.spring.io/browse/SPR-8539 and the SO thread @Value not resolved when using @PropertySource annotation. How to configure PropertySourcesPlaceholderConfigurer? for some more reference.

Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54
  • @ShiladittyaChakraborty Could you please confirm if this answer your query? – Madhu Bhat Jul 31 '18 at 06:28
  • @ShiladittyaChakraborty but you had mentioned that it's working fine by using the `PropertySourcesPlaceholderConfigurer` Bean method right? If you use that static method in your configuration class, the property should get wired with the `@Value` annotation. – Madhu Bhat Aug 01 '18 at 06:27
  • Its working in my Dev machine but not working UAT environment – Shiladittya Chakraborty Aug 01 '18 at 08:42
  • @ShiladittyaChakraborty you would need to check the difference between the Dev and UAT environment. This is beyond the scope of the question posted by you. Please accept the answer if it works in your Dev. – Madhu Bhat Aug 02 '18 at 05:13
  • @ShiladittyaChakraborty could you please accept if this answered your question? – Madhu Bhat Aug 21 '18 at 05:15
  • @ShiladittyaChakraborty if it works in your Dev env and not in your UAT env, you might need to check out the env differences. – Madhu Bhat Aug 21 '18 at 09:56