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 -->
<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>
jdbc.properties
##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
spring使用context:property-placeholder载不进属性问题的更多相关文章
- spring使用context:property-placeholder载不进属性问题 wangbiglei 发表于1年前 原 spring使用context:property-placeholder载不进属性问题
https://my.oschina.net/wangbiglei/blog/489583 http://www.cnblogs.com/leftthen/p/5615066.html
- spring 使用 context:property-placeholder 加载 多个 properties
一般使用PropertyPlaceholderConfigurer来替换占位符,例如: <bean class="org.springframework.beans.factory.c ...
- spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化
这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下: In spring wiring ,placeholder values are p ...
- spring bean的重新加载
架构体系 在谈spring bean的重新加载前,首先我们来看看spring ioc容器. spring ioc容器主要功能是完成对bean的创建.依赖注入和管理等功能,而这些功能的实现是有下面几个组 ...
- Spring源码剖析2:Spring IOC容器的加载过程
spring ioc 容器的加载流程 1.目标:熟练使用spring,并分析其源码,了解其中的思想.这篇主要介绍spring ioc 容器的加载 2.前提条件:会使用debug 3.源码分析方法:In ...
- Spring源码剖析3:Spring IOC容器的加载过程
本文转自五月的仓颉 https://www.cnblogs.com/xrq730 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https ...
- spring中 context:property-placeholder 导入多个独立的 .properties配置文件
spring中 context:property-placeholder 导入多个独立的 .properties配置文件? Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 o ...
- Spring BeanPostProcessor与动态加载数据源配置
前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverClass都通过运行时指定,而非由xml静态配置定死 ...
- Spring IoC BeanDefinition 的加载和注册
前言 本系列全部基于 Spring 5.2.2.BUILD-SNAPSHOT 版本.因为 Spring 整个体系太过于庞大,所以只会进行关键部分的源码解析. 本篇文章主要介绍 Spring IoC 容 ...
随机推荐
- MyBatis知识点整理
1.MyBatis一般使用步骤 1.1获取Configuration实例或编写配置文件 //获取Configuration实例的样例 TransactionFactory transactionFac ...
- 安全文件传输协议之SFTP的使用
一.SFTP概述 在前几篇文章,我们讲到了文件传输协议FTP(File Transfer Protocol),那也是使用比较广泛的文件服务器,但是我们需要知道,Linux系统并不自带FTP程序 如果要 ...
- C#初识LINQ
什么是LINQ 长期以来,开发社区形成以下的格局: 1.面向对象与数据访问两个领域长期分裂,各自为政. 2.编程语言中的数据类型与数据库中的数据类型形成两套不同的体系,例如: C#中字符串用strin ...
- SpringBoot集成SwaggerUI
1.在module下的pom.xml中引用相关插件 引用swagger插件并用参数化版本信息,如下 <?xml version="1.0" encoding="UT ...
- Java中正负数的存储方式-正码 反码和补码
Java中正负数的存储方式-正码 反码和补码 正码 我们以int 为例,一个int占用4个byte,32bits 0 存在内存上为 00000000 00000000 00000000 0000000 ...
- 简单桶排序(Bucket Sort)
1.基本思想 桶排序是将待排序集合中处于同一个值域的元素存放在同一个桶中1. 2.算法设计2 假设有一个班级有5个人,这次期末他们分别考了5分,2分,4分,5分,8分(满分为10分).需要将这些分数从 ...
- Cosmetic Bottles-Cosmetic Packaging Purpose: 5 Things
Packaging in the cosmetics industry is based on in-depth research. And how it helps to win the edge ...
- (VLAN)理解Hybrid接口的应用
实验三:理解Hybrid接口的应用 实验原理: 实验内容: 某企业二层网络使用两台S3700交换机S1和S2,且两台设备在不同的楼层.网络管理员规划了3个不同VLAN, HR部门使用VLAN 10,市 ...
- ubuntu 先安装php再安装apache后,php不解析
本来服务器是nginx ,把他改成了apache, 安装apache, sudo apt-get updatesudo apt-get install apache2 然后访问ip,不成功 vim / ...
- websocket协议实现
# websocket协议实现 1.抓包 wireshark规则: tcp.port == 9000 2. 结果解析 客户端请求: GET /ws/test_2 HTTP/1.1 Host: loca ...