搜尋

首頁  >  問答  >  主體

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 天前606

全部回覆(4)我來回復

  • PHP中文网

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

    1.這個首先確保下,你的properties位置沒有錯,如果你是跑單測時候,最好確保在test的路徑下也有。

    2.如果確認設定檔是OK的,spring會有一個工具類別NumberUtils來處理。 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}')}"

    會把string轉換成Intger類型,如果還是不行的話,可以試試看用SPEL語句,來強制轉換🎜 rrreee

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

    這個報錯這麼明顯。 。 。 init bean的時候spring自動轉換賦值macPic參數,但是String 轉int 異常了

    回覆
    0
  • ringa_lee

    ringa_lee2017-04-17 17:45:48

    exception is java.lang.NumberFormatException: For input string: "${maxPic}",你是不是寫的maxPic加了引號報錯了

    回覆
    0
  • 迷茫

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

    int 換成String
    private String maxPic.

    回覆
    0
  • 取消回覆