87

I'm trying to cast the output of a value to an integer:

@Value("${api.orders.pingFrequency}")
private Integer pingFrequency;

The above throws the error

org.springframework.beans.TypeMismatchException: 
    Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; 
nested exception is java.lang.NumberFormatException: 
    For input string: "(java.lang.Integer)${api.orders.pingFrequency}"

I've also tried @Value("(java.lang.Integer)${api.orders.pingFrequency}")

Google doesn't appear to say much on the subject. I'd like to always be dealing with an integer instead of having to parse this value everywhere it's used.

Workaround

I realize a workaround may be to use a setter method to run the conversion for me, but if Spring can do it I'd rather learn something about Spring.

5
  • Is the exception for the first try or the second? Mar 13, 2013 at 19:18
  • @SotiriosDelimanolis The first
    – Ben
    Mar 13, 2013 at 19:19
  • If you ever come across such a problem, it's most obviously, that you forgot to define the right PropertySource. You'll need: @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); }
    – Dudelilama
    Mar 1, 2016 at 8:39
  • 1
    it can also be caused by improper encoding of properties files (java expects ISO 8859-1)
    – Poutrathor
    May 6, 2020 at 18:09
  • 1
    Missing curly brace on end of property place holder caused this error for me :/
    – ununiform
    Jun 22, 2021 at 20:13

10 Answers 10

68

Assuming you have a properties file on your classpath that contains

api.orders.pingFrequency=4

I tried inside a @Controller

@Controller
public class MyController {     
    @Value("${api.orders.pingFrequency}")
    private Integer pingFrequency;
    ...
}

With my servlet context containing :

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

It worked perfectly.

So either your property is not an integer type, you don't have the property placeholder configured correctly, or you are using the wrong property key.

I tried running with an invalid property value, 4123;. The exception I got is

java.lang.NumberFormatException: For input string: "4123;"

which makes me think the value of your property is

api.orders.pingFrequency=(java.lang.Integer)${api.orders.pingFrequency}
0
40

I was looking for the answer on internet and I found the following

@Value("#{new java.text.SimpleDateFormat('${aDateFormat}').parse('${aDateStr}')}")
Date myDate;

So in your case you could try with this

@Value("#{new Integer('${api.orders.pingFrequency}')}")
private Integer pingFrequency;
5
  • 2
    This gives me SpelEvaluationException. Are you sure it works? Jan 20, 2016 at 8:53
  • I am also getting SpelEvaluationException
    – Saurabh
    Mar 10, 2016 at 7:04
  • 2
    Instead of 'new Integer.parseInt' using 'new Integer' worked for me.
    – Saurabh
    Mar 10, 2016 at 7:24
  • I am getting java.lang.NumberFormatException: For input string: &quot;${redis.server.port}&quot Nov 12, 2016 at 17:26
  • Make sure you are not leaving out the $ @Value("${api.orders.pingFrequency}") should work just fine Sep 9, 2019 at 22:57
9

I had the exact same situation. It was caused by not having a PropertySourcesPlaceholderConfigurer in the Spring context, which resolves values against the @Value annotation inside of classes.

Include a property placeholder to solve the problem, no need to use Spring expressions for integers (the property file does not have to exist if you use ignore-resource-not-found="true"):

<context:property-placeholder location="/path/to/my/app.properties"
    ignore-resource-not-found="true" />
7

If you want to convert a property to an integer from properties file there are 2 solutions which I found:

Given scenario: customer.properties contains customer.id = 100 as a field and you want to access it in spring configuration file as integer.The property customerId is declared as type int in the Bean Customer

Solution 1:

<property name="customerId" value="#{T(java.lang.Integer).parseInt('${customer.id}')}" />

In the above line, the string value from properties file is converted to int type.

solution 2: Use some other extension inplace of propeties.For Ex.- If your properties file name is customer.properties then make it customer.details and in the configuration file use the below code

<property name="customerId"  	value="${customer.id}" />

6

If you are using @Configuation then instantiate below static bean. If not static @Configutation is instantiated very early and and the BeanPostProcessors responsible for resolving annotations like @Value, @Autowired etc, cannot act on it. Refer here

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
   return new PropertySourcesPlaceholderConfigurer();
}
1
  • To add to the above, given that the property configurer @Bean would only appear in one configuration class, care should be taken when deleting any configuration class from your codebase, which could inadvertently remove this bean. Jul 25, 2019 at 7:02
1

I had the same issue I solved using this. Refer this Spring MVC: @Value annotation to get int value defined in *.properties file

@Value(#{propertyfileId.propertyName})

works

1
  • 1
    Sorry, but it does not work. #{...} gives me compile error. Instead, you could use int and not Integer... Jul 23, 2018 at 13:21
1

when use @Value, you should add @PropertySource annotation on Class, or specify properties holder in spring's xml file. eg.

@Component
@PropertySource("classpath:config.properties")
public class BusinessClass{
   @Value("${user.name}")
   private String name;
   @Value("${user.age}")
   private int age;
   @Value("${user.registed:false}")
   private boolean registed;
}

config.properties

user.name=test
user.age=20
user.registed=true

this works!

Of course, you can use placeholder xml configuration instead of annotation. spring.xml

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

This problem also occurs when you have 2 resources with the same file name; say "configurations.properties" within 2 different jar or directory path configured within the classpath. For example:

You have your "configurations.properties" in your process or web application (jar, war or ear). But another dependency (jar) have the same file "configurations.properties" in the same path. Then I suppose that Spring have no idea (@_@?) where to get the property and just sends the property name declared within the @Value annotation.

0

In my case, the problem was that my POST request was sent to the same url as GET (with get parameters using "?..=..") and that parameters had the same name as form parameters. Probably Spring is merging them into an array and parsing was throwing error.

0

Since using the @Value("new Long("myconfig")") with cast could throw error on startup if the config is not found or if not in the same expected number format

We used the following approach and is working as expected with fail safe check.

@Configuration()
public class MyConfiguration {

   Long DEFAULT_MAX_IDLE_TIMEOUT = 5l;

   @Value("db.timeoutInString")
   private String timeout;

   public Long getTimout() {
        final Long timoutVal = StringUtil.parseLong(timeout);
        if (null == timoutVal) {
            return DEFAULT_MAX_IDLE_TIMEOUT;
        }
        return timoutVal;
    }
}
   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.