---
title: "spring3.0.6 使用context:property-placeholder载不进属性"
author: "ssxxjjii"
date: "2012-12-15T14:43:09Z"
tags: []
categories: ["互联网"]
slug: "spring3-0-6-context-property-placeholder"
draft: false
lastmod: "2012-12-15T14:43:09Z"
id: 1748392
url: "https://tdcq.iteye.com/blog/1748392"
---

我用spring3.0.6+mybatis3.0.6+mybatis-spring1.0.2做了一个demo，我把数据库连接驱动放在src下，但是我在spring配置文件中加上 &lt;context:property-placeholder location="classpath:jdbc.properties" /&gt;，dataSource配置如下：

[view source](http://tdcq.iteye.com/admin/blogs/#viewSource "view source") [print](http://tdcq.iteye.com/admin/blogs/#printSource "print")[?](http://tdcq.iteye.com/admin/blogs/#about "?")

`01` `<bean` `id="dataSource"` `class="com.mchange.v2.c3p0.ComboPooledDataSource"` `destroy-method="close">`

`02` `<!--         <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> -->`

`03` `<!--         <property name="jdbcUrl" value="jdbc:oracle:thin:@10.1.1.3:1521:orcl"/> -->`

`04` `<!--         <property name="user" value="hyq"/> -->`

`05` `<!--         <property name="password" value="oracle"/> -->`

`06` `         ` 

`07` `<property` `name="driverClass"` `value="${jdbc.driverClass}"/>`

`08` `<property` `name="jdbcUrl"` `value="${jdbc.jdbcUrl}"/>`

`09` `<property` `name="user"` `value="${jdbc.user}"/>`

`10` `<property` `name="password"` `value="${jdbc.password}"/>`

`11` ` ` 

`12` `</bean>`

抛出异常如下：

[view source](http://tdcq.iteye.com/admin/blogs/#viewSource "view source") [print](http://tdcq.iteye.com/admin/blogs/#printSource "print")[?](http://tdcq.iteye.com/admin/blogs/#about "?")

`1` `2011-11-19 20:37:57 WARN [com.mchange.v2.c3p0.DriverManagerDataSource:107] - Could not load driverClass ${jdbc.driverClass}`

`2` `java.lang.ClassNotFoundException: ${jdbc.driverClass}`

而把上面那段注释的打开，相当于写死在配置文件中就正常。从上面异常我只能看出没加载

属性文件成功，属性文件如下：

[view source](http://tdcq.iteye.com/admin/blogs/#viewSource "view source") [print](http://tdcq.iteye.com/admin/blogs/#printSource "print")[?](http://tdcq.iteye.com/admin/blogs/#about "?")

`01` `## jdbc config`

`02` `jdbc.driverClass=oracle.jdbc.driver.OracleDriver`

`03` `jdbc.jdbcUrl=jdbc:oracle:thin:@10.1.1.3:1521:orcl`

`04` `jdbc.user=hyq`

`05` `jdbc.password=oracle`

`06` `jdbc.initialPoolSize=5`

`07` `jdbc.minPoolSize=5`

`08` `jdbc.maxPoolSize=30`

`09` `jdbc.acquireIncrement=5`

`10` `jdbc.maxIdleTime=10`

`11` `jdbc.maxStatements=0`

我是maven搭的，全放在/src/main/resources下，哪位帮我找找原因，谢谢！

 

\-------------------

楼上的解决方案比较一般，在3.1.1版本中配置的时候提供了以下：

 

&lt;bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"&gt;  

&lt;property name="basePackage" value="com.xxxx.dal.mapper" /&gt; 

&lt;property name="sqlSessionFactoryBeanName" value="ysSqlSessionFactory" /&gt;

 &lt;!-- &lt;property name="sqlSessionFactory" ref="ysSqlSessionFactory"&gt;&lt;/property&gt; --&gt;

 &lt;/bean&gt;

改用sqlSessionFactoryBeanName注入就没有问题(不要使用sqlSessionFactory属性注入，使用sqlSessionFactoryBeanName注入)，因为这时不会立即初始化sqlSessionFactory,传入的只是名字，非bean，所以不会引发提前初始化问题。。

 

[http://www.oschina.net/question/188964\_32305](http://www.oschina.net/question/188964_32305)