spring使用context:property-placeholder载不进属性问题

环境:spring3.1.1+mybatis3.2.8+mybatis-spring1.2.3

今天整合了SpringMVC + MyBatis,发现了一个问题,在这里做个记录,各位如果遇到相同的问题,可以参考下。

<!-- 引入jdbc配置文件 -->  
<context:property-placeholder location="classpath:prop/jdbc.properties" />

引入文件时出现下面的错误,提示dataSource中的使用资源文件中key对应的value值没有引入进来。

### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${jdbc.driver}'] with root cause java.lang.ClassNotFoundException: ${jdbc.driver}

首先确认jdbc.properties引入的路径没有问题。其次往下看。

解决案:

1. xml 头部将 default-autowire="byName"去掉。

2. 修改SqlSessionFactory。

<!-- 创建SqlSessionFactory,同时指定数据源 -->  
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --> <property name="mapperLocations" value="classpath*:maper/*.map.xml" /> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.com.mars.dao" /> </bean>

改成: 

<!-- 创建SqlSessionFactory,同时指定数据源 -->  
<bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --> <property name="mapperLocations" value="classpath*:maper/*.map.xml" /> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="org.com.mars.dao" /> <property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory" /> </bean>

 

这样,dataSource中就可以正常使用.properties文件中的key-value了。  

附:完整的配置文件,这个文件是用来完成spring和mybatis的整合的xml。

spring-resources.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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd" default-autowire="byName" default-lazy-init="false"> <!-- 自动扫描组件,需要把controller去掉,否则影响事务管理 --> <context:component-scan base-package="org.com.mars"> <context:exclude-filter type="regex" expression="org.com.mars.controller.*" /> </context:component-scan> <!-- 引入jdbc配置文件 --> <context:property-placeholder location="classpath:prop/jdbc.properties" /> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <

转载于:https://www.cnblogs.com/mochaMM/p/6932743.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值