spring4+mybatis3无法读取properties解决办法
来源:互联网 发布:阿里云服务器怎么退款 编辑:程序博客网 时间:2024/05/16 11:49
当把数据源配置信息改成从properties文件读取时错误提示:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [file:/D:/java/apache-tomcat-7.0.64/webapps/spring_mybatis/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:/java/apache-tomcat-7.0.64/webapps/spring_mybatis/WEB-INF/classes/config/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${driver}]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
db.properties
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/mybatisusername=rootpassword=123456
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd"><bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.isea533.mybatis.mapper" /><property name="properties"><value>mappers=tk.mybatis.mapper.common.Mapper</value></property></bean><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="ignoreResourceNotFound" value="true" /><property name="locations"><list><value>classpath:config/db.properties</value></list></property></bean><!-- 1. 数据源 : DriverManagerDataSource --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${driver}" /><property name="url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /></bean><!-- 2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源 MyBatis定义数据源,同意加载配置 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="configLocation" value="classpath:config/mybatis-config.xml" /></bean><!-- 3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory basePackage:指定sql映射文件/接口所在的包(自动扫描) --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.ihefe.mapper"></property><property name="sqlSessionFactory" ref="sqlSessionFactory"></property></bean><!-- 4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源 --><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 5. 使用声明式事务 transaction-manager:引用上面定义的事务管理器 --><tx:annotation-driven transaction-manager="txManager" /><!-- 注解扫描包 --><context:component-scan base-package="com.ihefe" /></beans>
解决:
applicationContext.xml中:
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
改成即可
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
原因:
在注入SqlSessionFactory时,可有两种方式,一种直接以对象的方式注入,此种方式会直接实例化dataSource,但是此时properties还没有进行解析,因此会出现上述的异常。另一种方式以属性名称注入成员变量,此方式注入时properties已经被解析替换,因此项目可以正常运行
- spring4+mybatis3无法读取properties解决办法
- Spring4+Mybatis不能读取 properties
- Spring4--Spring4整合mybatis3
- spring4+mybatis3的整合
- spring4整合mybatis3
- Maven3 + Spring4 + SpringMVC4 + Mybatis3
- Mybatis3+Spring4+SpringMVC4 整合
- Struts2+Spring4+myBatis3整合
- Mybatis3+Spring4+SpringMVC4 整合
- Mybatis3+Spring4+SpringMVC4 整合
- Maven3 + Spring4 + SpringMVC4 + Mybatis3
- Mybatis3+Spring4+SpringMVC4 整合
- spring4和mybatis3整合
- Mybatis3+Spring4+SpringMVC4 整合
- SpringMVC+Spring4+Mybatis3集成
- Mybatis3+Spring4+SpringMVC4 整合
- Spring4整合Mybatis3
- spring4 + mybatis3 整合
- 对称加密解密
- 剑指Offer:面试题7——用两个栈实现队列(java实现)
- VIM不正常退出产生的swp文件
- Zbrush中怎么通过Curves Tubes创建手指
- http协议
- spring4+mybatis3无法读取properties解决办法
- 模糊查询
- jQuery无法绑定新增元素click事件的解决方案
- mysql case
- http协议是什么?
- powerbuilder导出的sql建表语句在oracle中,出现“ora-00942: 表或视图不存在 ”
- Java多线程基础-1-基本慨念
- sidebarEffects.js 网页侧边栏过渡动画
- 数据类型转换 text 转 double 再保留两位小数