search

Home  >  Q&A  >  body text

java - 使用spring的@Value注解出错

容器启动的时候出错

配置文件如下:

报错信息如下:

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}"

怎么解决呢?

高洛峰高洛峰2887 days ago609

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:45:48

    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

    reply
    0
  • PHPz

    PHPz2017-04-17 17:45:48

    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

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:45:48

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

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:45:48

    int replaced by String
    private String maxPic.

    reply
    0
  • Cancelreply