Georgi Michev
Georgi Michev

Reputation: 894

get an int value with @Value

I have application.properties file and I successfully get String values from it with @Value. I am having problems getting an int from it.

jedisHostName=127.0.0.1
redisPort=6379

In my config class I have

@Value("${jedisHostName}")
private String hostName;

and it works fine, but when I try to

@Value("#{new Integer.parseInt('${redisPort}')}")
private Integer redisPort;

I get

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'secret***': Unsatisfied dependency expressed through field 'redisPort';

I am also trying only

@Value("#{new Integer('${redisPort}')}")

but I get same exception. I am even trying to simply do a

@Value("${redisPort}")
private String redisPort;

int jedisPort = Integer.parseInt(redisPort.trim());

but then I get

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'secret***' defined in file [secret***.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate **** Constructor threw exception; nested exception is java.lang.NullPointerException

I have normal class names but I use "secret***" just for the example

Upvotes: 4

Views: 11404

Answers (1)

NiVeR
NiVeR

Reputation: 9806

Simply:

@Value("${redisPort}")
private Integer redisPort;

should work. You should not do any parsing yourself, it will be taken care for you by higher forces.

Upvotes: 9

Related Questions