使用yaml配置启动发生ClassCastException错误
项目使用spring boot 1.5.7,application.yml中的pagehelper配置如下时
pagehelper:
auto-dialect: true
close-conn: true
启动出现org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration': Invocation of init method failed; nested exception is java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String。 苦苦思索查找几日,发现问题出在PageHelperProperties类中。 现在我的折中做法是
pagehelper:
auto-dialect: !!str true
close-conn: !!str true
但还请作者稍稍处理一下这个问题。
好,下个版本处理这个问题。
请问下,pagehelper-spring-boot-starter:1.2.3 ,还是报这个错,目前折中做法也是
reasonable: !!str true
support-methods-arguments: !!str true
引入com.google.guava:guava:19.0包也可以解决这个问题
pagehelper-spring-boot-starter:1.2.3 以下配置会报错
pagehelper:
page-size-zero: true
看到如下代码片段
@Bean
@ConfigurationProperties(prefix = PageHelperProperties.PAGEHELPER_PREFIX)
public Properties pageHelperProperties() {
return new Properties();
}
不知作者写入额外的属性是作为什么用途,spring在处理application.yml时注册了许多类型转换插件,其中包括了StringToBooleanConverter,该组件会尝试将true翻译为Boolean注入到Properties类中,留意到作者在处理属性的时候没有做key的转换而是全部添加到properties中
properties.putAll(pageHelperProperties());
properties.putAll(this.properties.getProperties());
而mybatis在读取properties时,强制转换为String再按方法类型进行注入,强制转换的过程中会发生Boolean转String的错误 jar:mybatis-3.4.5.jar class org.apache.ibatis.mapping.CacheBuilder line 147
String value = (String) entry.getValue();
Spring Boot 版本的问题,别用 1.5.0 和 1.5.1 两个版本。
在 spring-boot 1.5.14 版本也出现了这个问题 java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String。
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
但奇怪的是 reasonable: true 就没问题,support-methods-arguments: true 就会报错,用support-methods-arguments: 'true', 或者support-methods-arguments: !!str true 甚至 supportMethodsArguments: true 可以解决,还请作者再看下这个问题。