spring context:property-placeholder无法加载配置文件,导致属性无效
来源:互联网 发布:张国荣和四大天王 知乎 编辑:程序博客网 时间:2024/05/16 17:44
在把现有项目转为maven项目时,启动报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in URL [file:/D:/workspace/datasync/target/datasync/WEB-INF/classes/config/applicationContext.xml]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [file:/D:/workspace/datasync/target/datasync/WEB-INF/classes/config/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in URL [file:/D:/workspace/datasync/target/datasync/WEB-INF/classes/config/applicationContext.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 'acquireIncrement'; nested exception is java.lang.NumberFormatException: For input string: "${c3p0.acquireIncrement}"
最后找到原因,是jdbc.properties中属性压根没有加载,导致把配置的变量名直接当成参数值,才会报错。
网上找到错误根本原因,是因为mybatis生成sqlSessionFactory比PropertyPlaceholderConfigurer初始化早的原因,导致变量没有及时加载进来
引用:
在spring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 进行自动扫描的时候,设置了sqlSessionFactory 的话,可能会导致 PropertyPlaceholderConfigurer失效,也就是用${jdbc.username}这样之类的表达式,将无法获取到properties文件里的内容。 导致这一原因是因为,MapperScannerConigurer实际是在解析加载bean定义阶段的,这个时候要是设置sqlSessionFactory的话,会导致提前初始化一些类,这个时候,PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了。 但如果不设置sqlSessionFactory 属性的话,就必须要保证sessionFactory在spring中名称一定要是sqlSessionFactory ,否则就无法自动注入。又或者直接定义 MapperFactoryBean ,再或者放弃自动代理接口方式。
解决方案:
但是在原来项目启动时是不报错的,考虑转换maven项目,只有jar包会更改,拿现有jar包和原jar包对比,发现mybatis-xxx.jar和mybatis-spring-xxx.jar和原有项目的jar包版 本不一样,修改jar包版本后,问题解决。
- spring context:property-placeholder无法加载配置文件,导致属性无效
- context:property-placeholder,spring配置文件加载属性信息
- spring配置文件中的context:property-placeholder属性
- spring配置文件中的context:property-placeholder属性
- Spring配置文件<context:property-placeholder>
- spring配置文件<context:property-placeholder>
- 关于spring配置文件<context:property-placeholder>
- Spring配置文件<context:property-placeholder>标签使用
- spring的context:property-placeholder属性
- spring的context:property-placeholder属性
- spring 使用context:property-placeholder载不进属性 属性加载之前就读取属性变量
- 关于 Spring 中使用 context:property-placeholder 属性占位符设置配置文件
- MapperScannerConfigurer与<context:property-placeholder>加载顺序导致异常解决办法
- spring 配置文件中的占位符 使用 context:property-placeholder
- Spring配置文件<context:property-placeholder>标签使用漫谈
- spring 配置文件中的占位符 使用 context:property-placeholder
- spring配置文件中<context:property-placeholder />的使用
- spring 配置文件中的占位符 使用 context:property-placeholder
- Android 你必须了解的网络框架Retrofit2.0
- Angular 指令好文章收录(三)
- Head Fist 前言
- struts2类型转换和验证
- cs231深度学习课程,注意点总结(2)
- spring context:property-placeholder无法加载配置文件,导致属性无效
- rang xrange
- secureCTR破解
- rails 网络接口模块
- Algorithms: Design and Analysis, Part 1, Programming Assignment #5
- js弹出框、对话框、提示框、弹窗总结
- 十分钟教程,eclipse配置maven + 创建maven项目
- 面试 程序媛的博客
- Siege web 压测工具代码分析