问题:写了一个新的dao接口,进行单元测试时提示:

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: "${maxActive}"

原配置datasource时使用了占位符,该提示是在解析占位符${maxActive}时未找到对应的属性。

单元测试加载properties使用@PropertySource(value = {"classpath*:jdbc.properties"})注解加载配置文件。

在确认自己properties文件路径是正确的且存在该属性值后,在网上找到相应的资料如https://my.oschina.net/u/1455908/blog/215953说的是在配置mybatis的MapperScannerConigurer时会优先于@PropertySource注解解析占位符,由于占位符未进行解析,直接使用了“${maxActive}”了该字符串作为该配置项的值。也就是报错所说的“${maxActive}”这个字符串无法转化成对应的int数值。

解决问题

将配置文件的加载由原先使用注解@PropertySource(value = {"classpath*:jdbc.properties"})改成如下:

	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"></property>
</bean>

原先MapperScannerConfigurer的配置没有做修改,如下:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.**.dao,com.**.mapper,com.**.test.**.mapper" />
<!--网上说这个name属性值要配置成这个sqlSessionFactoryBeanName名字,我恰好配的就是这个,所以我这里不需要改-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

这样该问题解决。但疑问依然存在,为何@PropertySource这个注解没有ignoreUnresolvablePlaceholders这个属性可以进行配置,并且用xml的方式又能正确解析。

Spring与Mybatis整合占位符无法解析问题的更多相关文章

  1. Spring Cloud Finchley.SR1 版本的坑:placeholer占位符无法解析!

    接入nacos 之后,想把所有的配置丢上去. 启动程序是: @EnableDiscoveryClient @RestController @ComponentScan(basePackages = { ...

  2. spring源码分析之配置文件名占位符的解析(一)

    一.直接写个测试例子 package com.test; import org.junit.Test; import org.springframework.context.ApplicationCo ...

  3. dubbo注册中心占位符无法解析问题

    dubbo注册中心占位符无法解析问题 1.背景 最近搞了2个老项目,想把他们融合到一起.这俩项目情况简介如下: 项目一:基于SpringMVC + dubbo,配置读取本地properties文件,少 ...

  4. 九 spring和mybatis整合

    1       spring和mybatis整合 1.1     整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用Sq ...

  5. 框架篇:Spring+SpringMVC+Mybatis整合开发

    前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...

  6. SpringMVC, Spring和Mybatis整合案例一

    一  准备工作 包括:spring(包括springmvc).mybatis.mybatis-spring整合包.数据库驱动.第三方连接池. 二  整合思路 Dao层: 1.SqlMapConfig. ...

  7. spring, spring mvc, mybatis整合文件配置详解

    转自:http://www.cnblogs.com/wxisme/p/4924561.html 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用 ...

  8. ssm之spring+springmvc+mybatis整合初探

    1.基本目录如下  2.首先是向lib中加入相应的jar包  3.然后在web.xml中加入配置,使spring和springmvc配置文件起作用. <?xml version="1. ...

  9. dubbo注册中心占位符无法解析问题(二)

    dubbo注册中心占位符无法解析问题 前面分析了dubbo注册中心占位符无法解析的问题. 并给出了2种解决办法: 降低mybatis-spring的版本至2.0.1及以下 自定义MapperScann ...

随机推荐

  1. WPF DataGrid ScrollBar Style

    效果图如下 代码 <DataGrid.Resources> <Style TargetType="{x:Type ScrollBar}"> <Sett ...

  2. day58 前端收尾

    目录 一.jQuery结束 1 阻止后续事件执行 2 阻止事件冒泡 3 事件委托 4 页面加载 5 动画效果 6 补充知识点 二.前端框架Bootstrap 1 布局容器 2 栅格系统 3 栅格参数 ...

  3. requests接口自动化2-url里不带参数的get请求

    最常用的是get,post请求,然后是put,delete,其他方法很少用 1. get请求几种方式 1.1.url里不带参数的get请求 接口请求fiddler返回内容: import reques ...

  4. java 面向对象(三十):异常(三) 手动抛出异常对象

    1.使用说明在程序执行中,除了自动抛出异常对象的情况之外,我们还可以手动的throw一个异常类的对象. 2.[面试题] throw 和 throws区别:throw 表示抛出一个异常类的对象,生成异常 ...

  5. Python函数07/有参装饰器/多个装饰器装饰一个函数

    Python函数07/有参装饰器/多个装饰器装饰一个函数 目录 Python函数07/有参装饰器/多个装饰器装饰一个函数 内容大纲 1.有参装饰器 2.多个装饰器装饰一个函数 3.今日总结 3.今日练 ...

  6. Oracle-常见的命令

    --------------输入一下指令,按下快捷键 F8 select * from emp; --------------创建表格 create table 表名( 字段名1 数据类型1, 字段名 ...

  7. mysql中常见约束

    #常见约束 /* 含义:一种限制,用于限制表中的数据,为了保证表中的数据的准确和可靠性 分类:六大约束 NOT NULL:非空,用于保证该字段的值不能为空 比如姓名.学号等 DEFAULT:默认,用于 ...

  8. Spring用到了那些注解?

    一:@Autowired(按类型注入)1.1通过 @Autowired的使用来消除 set ,get方法.@Autowiredprivate Dao dao;这样就可以删除set ,get方法和spr ...

  9. Ethical Hacking - GAINING ACCESS(13)

    CLIENT SIDE ATTACKS Backdoor delivery method2 - backdooring exe downloads Backdoor any exe the targe ...

  10. Linux下Storm2.1.0的伪分布式安装

    官方下载网址:http://storm.apache.org/downloads.html 1.第一步我们先从官网下载解压包  2.然后进行解压 3.配置环境变量 在profile里面插入如下格式语句 ...