java - 使用spring的@Value注解出错
高洛峰
高洛峰 2017-04-17 17:43:48
0
4
596

容器启动的时候出错

配置文件如下:

报错信息如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'communityController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private int cn.wanjubang.toy.core.controller.Basecontroller.maxPic; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "${maxPic}"

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private int cn.wanjubang.toy.core.controller.Basecontroller.maxPic; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "${maxPic}"

怎么解决呢?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(4)
Ty80

1. First of all, make sure that the location of your properties is correct. If you are running a single test, it is best to make sure that it is also in the test path.

2. If the configuration file is confirmed to be OK, spring will have a tool class NumberUtils to handle it. NumberUtils来处理。

            return (T) (isHexNumber(trimmed) ? Byte.decode(trimmed) : Byte.valueOf(trimmed));
        }
        else if (targetClass.equals(Short.class)) {
            return (T) (isHexNumber(trimmed) ? Short.decode(trimmed) : Short.valueOf(trimmed));
        }
        else if (targetClass.equals(Integer.class)) {
            return (T) (isHexNumber(trimmed) ? Integer.decode(trimmed) : Integer.valueOf(trimmed));
        }

会把string转换成Intger类型,如果还是不行的话,可以试试用SPEL

"#{T(java.lang.Integer).parseInt('${maxPic}')}"
It will convert the string into Intger type. If it still doesn’t work, you can try to use the SPEL statement to force the conversion🎜 rrreee
PHPzhong

Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "${maxPic}"

This error is so obvious. . . When init bean, spring automatically converts and assigns macPic parameters, but the conversion from String to int is abnormal

左手右手慢动作

exception is java.lang.NumberFormatException: For input string: "${maxPic}", did you write maxPic with quotation marks and reported an error

迷茫

int replaced by String
private String maxPic.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template