java - 使用spring的@Value注解出错
高洛峰
高洛峰 2017-04-17 17:43:48
[Java讨论组]

容器启动的时候出错

配置文件如下:

报错信息如下:

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教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(4)
PHP中文网

1.这个首先确保下,你的properties位置没有错,如果你是跑单测时候,最好确保在test的路径下也有。

2.如果确认配置文件是OK的,spring会有一个工具类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}')}"
PHPz

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 异常了

ringa_lee

exception is java.lang.NumberFormatException: For input string: "${maxPic}",你是不是写的maxPic加了引号报错了

迷茫

int 换成String
private String maxPic.

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号