Spring和Mybatis整合时无法读取properties的处理方案
来源:互联网 发布:树莓派gpio编程工具 编辑:程序博客网 时间:2024/06/05 09:10
版本:Spring:3.1.4、Mybatis:3.1.1、Mybatis-Spring:1.1.1;
背景:
config.properties配置文件信息
- ##数据库(mysql|mssql|oracle...)
- environment=mysql
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://127.0.0.1:3306/portal?useUnicode=true&characterEncoding=utf8
- jdbc.user=root
- jdbc.password=root
- #初始化连接(根据实际访问量设置大小)
- jdbc.initialSize=10
- #最大空闲连接(根据实际访问量设置大小)
- jdbc.maxIdle=50
- #最小空闲连接(根据实际访问量设置大小)
- jdbc.minIdle=10
- #最大连接数量(根据实际访问量设置大小)
- jdbc.maxActive=200
Spring配置信息(截取部分)
- <context:property-placeholder location="classpath:config.properties" />
- <!--创建jdbc数据源 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="${jdbc.driver}"/>
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.user}"/>
- <property name="password" value="${jdbc.password}"/>
- <property name="initialSize" value="${jdbc.initialSize}"/>
- <property name="maxIdle" value="${jdbc.maxIdle}"/>
- <property name="minIdle" value="${jdbc.minIdle}"/>
- <property name="maxActive" value="${jdbc.maxActive}"/>
- <property name="removeAbandoned" value="true"/>
- <property name="removeAbandonedTimeout" value="120"/>
- <property name="maxWait" value="3000"/>
- </bean>
- <!-- 创建SqlSessionFactory,同时指定数据源 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="configLocation" value="classpath:mybatis-config.xml"/>
- <property name="mapperLocations" value="classpath*:com.anly.portal.*.mapper/*Mapper.xml" />
- </bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <!-- 此处扫描的是Mapper接口 -->
- <property name="basePackage" value="com.anly.portal.*.mapper"/>
- <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
- </bean>
此时,启动会报异常,${jdbc.driver}这样的表达式获取不到properties里面的值,因为MapperScannerConigurer实际是在解析加载bean定义阶段的,这个时候要是设置sqlSessionFactory的话,会导致提前初始化一些类,这个时候,PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了,解决的办法如下:
方法一:
修改<property name="sqlSessionFactory" ref="sqlSessionFactory"/>为<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
原理:使用sqlSessionFactoryBeanName注入,不会立即初始化sqlSessionFactory, 所以不会引发提前初始化问题。
方法二:
直接删掉<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
注意:在没有配置这一行时,必须配置一个以sqlSessionFactory命名的org.mybatis.spring.SqlSessionFactoryBean。
一个完成的配置示例:
<?xml version="1.0" encoding="UTF-8"?><!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 --> <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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 属性文件 --> <context:property-placeholder location="classpath:oracle.properties" /> <!-- 数据源 p:initialSize="20" p:maxActive="100" p:maxIdle="30" p:maxWait="1000" p:poolPreparedStatements="true" destroy-method="close"--> <bean id="dataSource" destroy-method="close"class="org.apache.commons.dbcp.BasicDataSource" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" p:defaultAutoCommit="true" p:poolPreparedStatements="true"> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- dataSource属性指定要用到的连接池 --> <property name="dataSource" ref="dataSource"/> <!-- configLocation属性指定mybatis的核心配置文件 --> <property name="configLocation" value="classpath:Configuration.xml"/> <!-- 所有配置的mapper文件 --> <property name="mapperLocations" value="classpath*:com/winit/mapper/*.xml"/> </bean> <!-- 注入mybatis的数据库操作接口dao到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.winit.dao"/> </bean> <!-- 扫描com这个包里的所有类,把里面配上相应注解的类全都放在容器中进行管理(如果这里不加的话,单元测试会获取不到) --> <context:component-scan base-package="com.winit"/> </beans>
1 0
- Spring和Mybatis整合时无法读取properties的处理方案
- Spring和Mybatis整合时无法读取properties的处理方案
- Spring和Mybatis整合时无法读取properties的处理方案
- Spring和Mybatis整合时无法读取properties的处理方案
- Spring和Mybatis整合时无法读取properties的处理方案
- Spring和Mybatis整合时无法读取properties的处理方案
- Spring和Mybatis整合时无法读取properties的处理方案
- spring+mybatis整合时占位符无法读取jdbc.properties的问题
- mybatis与spring整合时读取properties问题的解决
- Spring整合myBatis时无法读取placeholder的问题解决
- 关于Spring与mybatis整合无法读取配置文件的问题
- Spring SpringMvc 3.0 + MyBatis 整合--补充关于.properties文件的读取
- MyBatis+Spring整合方案
- spring和mybatis整合加载jdbc.properties异常
- spring 和 mybatis 的整合
- Mybatis 和 Spring 的整合
- Spring和Mybatis的整合
- Mybatis和Spring的整合
- matlab 图像分割 hough 霍夫变换
- CRC算法
- 好用的第三方--IQKeyboardManager
- access数据库编码转换:将将UTF8替换为GB2312
- Linux下更改目录及其下的子目录和文件的访问权限
- Spring和Mybatis整合时无法读取properties的处理方案
- 题理综
- Spring 学习理解及第一个demo
- 51nod 1489 蜥蜴和地下室(DFS)
- matlab 图片显示 imshow axis title xlabel ylabel plot
- codeforces706B(二分)之Interesting drink
- hdu 5855 Less Time, More profit(最大权闭合子图)
- [LeetCode] 97. Interleaving String
- windows 下安装Django小记