java.lang.NumberFormatException: For input string: "${jdbc.maxActive}"

一、问题

使用SpringMVC和MyBatis整合,将jdbc配置隔离出来的时候出现下面的错误,百度了很久没有找到解决方法,回家谷歌下,就找到解决方法了,不得不说谷歌就是强大,不废话,下面是具体的错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘mySqlDataSource‘ defined in ServletContext resource [/WEB-INF/classes/config/spring/daoSource.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [int] for property ‘maxActive‘; nested exception is java.lang.NumberFormatException: For input string: "${jdbc.maxActive}"
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
    ... 41 more
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [int] for property ‘maxActive‘; nested exception is java.lang.NumberFormatException: For input string: "${jdbc.maxActive}"
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:596)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1527)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1486)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    ... 47 more
Caused by: java.lang.NumberFormatException: For input string: "${jdbc.maxActive}"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:569)
    at java.lang.Integer.valueOf(Integer.java:766)
    at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:208)
    at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:113)
    at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:468)
    at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:441)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:199)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576)
    ... 53 more

二、出现原因

  这个问题是无法识别占位符,就是在加载过程中直接把 $ { jdbc.maxActive }当做字符串处理了。myabatis使用MapperScannerConfigurer扫描模式后他会优先于PropertyPlaceholderConfigurer执行,所以这个时候,${jdbc.maxActive }还没有被properties文件里面的值所替换,就出现TypeMismatchException,然后就异常了。

三、解决方法

    <!-- 配置sqlSessionFactory -->
    <bean id="mysqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="mySqlDataSource" />
         <property name="configLocation" value="/WEB-INF/classes/config/db/mybatis-config.xml" />
        <property name="mapperLocations">
            <list>
                <value>classpath:com/xsjt/dao/**/*Mapper.xml</value>
            </list>
        </property>
    </bean>

     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
     <bean name="mysqlMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="annotationClass" value="org.springframework.stereotype.Repository" />
        <property name="basePackage" value="com.rxwx.dao" />
        <!-- 这里 用sqlSessionFactoryBeanName,而不是sqlSessionFactory,接下来用value而不是ref -->
        <property name="sqlSessionFactoryBeanName" value="masterSqlSessionFactory" />
    </bean>

MapperScannerConfigurer扫描Dao的 配置中要使用sqlSessionFactoryBeanName,而不是sqlSessionFactory,接下来用value而不是ref。

原文地址:https://www.cnblogs.com/xbq8080/p/8111230.html

时间: 2024-10-13 04:13:18

java.lang.NumberFormatException: For input string: "${jdbc.maxActive}"的相关文章

java.lang.NumberFormatException: For input string:""

今天写了个java的小程序在运行的时候抛出下面的异常:java.lang.NumberFormatException: For input string: "" .定位到源程序中出错的地方: String[] cols = key.toString().split("    ");   return Integer.parseInt(cols[0]) % numPartitions; 通过查询java API 以及百度,了解到抛NumberFormatExcepti

Caused by: java.lang.NumberFormatException: For input string: &quot;&quot;

1.错误描述 java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:453) at java.lang.Long.parseLong(Long.java:483) at org.springfram

SimpleDateFormat高并发下异常java.lang.NumberFormatException: For input string: &quot;&quot;

1. 原因 SimpleDateFormat(下面简称sdf)类内部有一个Calendar对象引用,它用来储存和这个sdf相关的日期信息,例如sdf.parse(dateStr), sdf.format(date) 诸如此类的方法参数传入的日期相关String, Date等等, 都是交友Calendar引用来储存的.这样就会导致一个问题,如果你的sdf是个static的, 那么多个thread 之间就会共享这个sdf, 同时也是共享这个Calendar引用, java.lang.NumberFo

java.lang.NumberFormatException: For input string: &quot;undefined&quot;

在将字符串转换为数字时导致此错误,解决此问题的思路:1.添加Try catch语句,2.判断字符串是否为数字,将介绍java中判断字符串是否为数字的方法的几种方法. 完整错误信息: java.lang.NumberFormatException: For input string: "undefined" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.pars

java.lang.NumberFormatException: For input string: &quot;title&quot;异常

java.lang.NumberFormatException: For input string: "title" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:492) at java.lang.Integer.parseInt(Integer.java:527) at javax.

java.lang.NumberFormatException: For input string:&quot;filesId&quot;

做项目时候,页面获取出现了这个问题.找了好久一直以为是我字段或者是数据库字段问题导致引起的. 最后才发现是 struts2中jsp我写错了一个参数,一直导致报错.后来改了就好了. 当大家遇到这个问题的时候,记得先看看是不是这个问题,不是的话可以再debug分析下. 对jsp更改的地方,以后要多多注意.自己不要再犯这种低级错误 就是因为这个 变量写错了,导致他找不到塞入数据的泪,所以,下面的一些列 ${freemarkerdetailList.xxxxx}都报 java.lang.NumberFo

Caused by: java.lang.NumberFormatException: For input string: &amp;quot;&amp;quot;

1.错误描写叙述 java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:453) at java.lang.Long.parseLong(Long.java:483) at org.springfr

解决java.lang.NumberFormatException: For input string: &quot;id&quot;

今天,项目突然报"java.lang.NumberFormatException:For input string:"id"",项目框架是spring,springmvc,hibernate,大致意思是类型转换错误,我的id是Integer类型,报错在<c:foreach>里的option里获取值得时候出问题,说明数据类型不对,就去后台dao去找,查看我的sql,一看我查的是表的部分字段,用的hibernate框架,返回的一个List<object

java.lang.NumberFormatException: For input string: &quot;?0&quot;

在使用JAVA IP地址转成长整型方法时出现此错误 这里对场景进行记录,以备日后遇到类似的错误,参考如何解决 错误输出: 错误原因:怀疑此处为"\0"字符 解决方法: java.lang.NumberFormatException: For input string: "?0"