spring使用context-property-placeholder载不进属性问题
来源:互联网 发布:windows server iscsi 编辑:程序博客网 时间:2024/06/06 05:55
环境:spring3.1.1+mybatis3.2.8+mybatis-spring1.2.3
今天整合了SpringMVC + MyBatis,发现了一个问题,在这里做个记录,各位如果遇到相同的问题,可以参考下。
?
1
2
<!-- 引入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。
将
?
1
2
3
4
5
6
7
8
9
10
11
<!-- 创建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
>
?
1
2
3
4
5
6
7
8
9
10
11
12
<!-- 创建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"
/>//value不能是ref,ref也会报错
</
bean
>
附:完整的配置文件,这个文件是用来完成spring和mybatis的整合的xml。
spring-resources.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?
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 -->
<
bean
id
=
"transactionManager"
class
=
"org.springframework.jdbc.datasource.DataSourceTransactionManager"
>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
</
bean
>
<!-- 声明式事务管理 -->
<
aop:config
>
<
aop:advisor
pointcut
=
"execution(* org.com.mars.service..*.*(..))"
advice-ref
=
"myAdvice"
/>
</
aop:config
>
<
tx:advice
id
=
"myAdvice"
transaction-manager
=
"transactionManager"
>
<
tx:attributes
>
<
tx:method
name
=
"find*"
propagation
=
"SUPPORTS"
read-only
=
"true"
/>
<
tx:method
name
=
"query*"
propagation
=
"SUPPORTS"
read-only
=
"true"
/>
<
tx:method
name
=
"list*"
propagation
=
"SUPPORTS"
read-only
=
"true"
/>
<
tx:method
name
=
"create*"
propagation
=
"REQUIRED"
/>
<
tx:method
name
=
"save*"
propagation
=
"REQUIRED"
/>
<
tx:method
name
=
"modify*"
propagation
=
"REQUIRED"
/>
<
tx:method
name
=
"update*"
propagation
=
"REQUIRED"
/>
<
tx:method
name
=
"delete*"
propagation
=
"REQUIRED"
/>
<
tx:method
name
=
"*"
propagation
=
"SUPPORTS"
read-only
=
"true"
/>
</
tx:attributes
>
</
tx:advice
>
<!-- 创建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
>
<!-- 可通过注解控制事务 -->
<
tx:annotation-driven
/>
<!-- 数据库连接池 -->
<
bean
id
=
"dataSource"
class
=
"org.apache.commons.dbcp.BasicDataSource"
destroy-method
=
"close"
>
<!-- Connection Info -->
<
property
name
=
"driverClassName"
value
=
"${jdbc.driver}"
/>
<
property
name
=
"url"
value
=
"${jdbc.url}"
/>
<
property
name
=
"username"
value
=
"${jdbc.username}"
/>
<
property
name
=
"password"
value
=
"${jdbc.password}"
/>
<!-- Connection Pooling Info -->
<
property
name
=
"maxActive"
value
=
"${dbcp.maxActive}"
/>
<
property
name
=
"maxIdle"
value
=
"${dbcp.maxIdle}"
/>
<
property
name
=
"defaultAutoCommit"
value
=
"false"
/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<
property
name
=
"timeBetweenEvictionRunsMillis"
value
=
"3600000"
/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<
property
name
=
"minEvictableIdleTimeMillis"
value
=
"3600000"
/>
</
bean
>
</
beans
>
?
1
2
3
4
5
6
7
8
9
10
11
12
##jdbc
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
#dbcp settings
dbcp.initialSize=5
dbcp.maxActive=20
dbcp.maxIdle=10
jdbc.dbType=mysql
感谢热心的前辈技术分享!本文如有不正,请指出。
参考资料:
http://www.oschina.net/question/188964_32305
以上。
阅读全文
0 0
- 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属性
- 使用context:property-placeholder载不进属性原因分析与解决方案
- spring3+mybatis 使用MapperScannerConfigurer时context:property-placeholder载不进属性
- spring中<context:property-placeholder>使用
- Spring配置文件<context:property-placeholder>标签使用
- spring中context:property-placeholder/元素 --spring 属性文件
- spring 和mybatis整合时 使用context:property-placeholder载不进属性 还报org.springframework.beans.factory.BeanCrea
- 关于 Spring 中使用 context:property-placeholder 属性占位符设置配置文件
- context:property-placeholder,spring配置文件加载属性信息
- spring context:property-placeholder无法加载配置文件,导致属性无效
- Photoshop快捷键大全
- qt4.8.7交叉编译
- 数据结构(Java)--基础概念
- python+opencv实现人头检测
- 【剑指offer】题22:栈的压入、弹出序列
- spring使用context-property-placeholder载不进属性问题
- JS遇坑日记----持续更新
- CodeForces820B【set.lower_bound】
- 构造方法的重载要点
- spring配置datasource三种方式
- PAL分辨率和NTSC分辨率
- mysql二次写缓存
- 【加密解密】-AES加密解密实现
- 浮点数的秘密