我有 application.properties 文件,我成功地使用 @Value 从中获取了字符串值。我在从中获取 int 时遇到问题。jedisHostName=127.0.0.1redisPort=6379在我的配置类中,我有@Value("${jedisHostName}")private String hostName;它工作正常,但是当我尝试@Value("#{new Integer.parseInt('${redisPort}')}")private Integer redisPort;我得到org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'secret***': Unsatisfied dependency expressed through field 'redisPort';我也只尝试@Value("#{new Integer('${redisPort}')}")但我得到同样的例外。我什至试图简单地做一个@Value("${redisPort}")private String redisPort;int jedisPort = Integer.parseInt(redisPort.trim());但后来我得到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我有普通的类名,但我使用“secret***”作为例子
1 回答
一只斗牛犬
TA贡献1784条经验 获得超2个赞
简单地:
@Value("${redisPort}")
private Integer redisPort;
应该管用。你不应该自己做任何解析,它会被更高的力量照顾你。
添加回答
举报
0/150
提交
取消