Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 823
Georgi Michev Avatar asked Oct 14 '25 08:10

Georgi Michev


1 Answers

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.

like image 189
NiVeR Avatar answered Oct 16 '25 21:10

NiVeR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!