Spring及Mybatis整合占位符解析失败问题解决
作者:来自海上的鱼 时间:2022-08-13 06:42:49
问题:写了一个新的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的方式又能正确解析。
来源:https://www.cnblogs.com/frankwin608/p/11870274.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot项目在IntelliJ IDEA中如何实现热部署
![](https://img.aspxhome.com/file/2023/4/58584_0s.png)
Java实现的不同图片居中剪裁生成同一尺寸缩略图功能示例
Spark网站日志过滤分析实例讲解
![](https://img.aspxhome.com/file/2023/7/60927_0s.png)
Java超详细分析@Autowired原理
![](https://img.aspxhome.com/file/2023/9/60059_0s.png)
一篇文章带你入门Java Script
spring boot如何实现切割分片上传
![](https://img.aspxhome.com/file/2023/7/57487_0s.png)
IDEA中已配置阿里镜像但maven无法下载jar包的问题及解决方法
![](https://img.aspxhome.com/file/2023/9/64499_0s.jpg)
Java 实现网络爬虫框架详细代码
SpringBoot 整合 Shiro 密码登录的实现代码
Spark SQL的自定义函数UDF使用
Mybatis单个参数的if判断报异常There is no getter for property named 'xxx' in 'class java.lang.Integer'的解决方案
![](https://img.aspxhome.com/file/2023/8/58668_0s.png)
jdbc和mybatis的流式查询使用方法
基于Java利用static实现单例模式
java抓取网页或文件中的邮箱号码
![](https://img.aspxhome.com/file/2023/9/62119_0s.jpg)
SpringMVC @RequestBody自动转json Http415错误的解决
![](https://img.aspxhome.com/file/2023/0/63070_0s.jpg)
Mybatis-plus基于redis实现二级缓存过程解析
![](https://img.aspxhome.com/file/2023/1/63151_0s.png)
Java C++ 算法leetcode828统计子串中唯一字符乘法原理
![](https://img.aspxhome.com/file/2023/8/61118_0s.png)
Java基础之位运算知识总结
![](https://img.aspxhome.com/file/2023/3/66043_0s.png)
java开发之Jdbc分页源码详解
Java封装公共Result结果返回类的实现
![](https://img.aspxhome.com/file/2023/2/57502_0s.jpg)