Spring和Mybatis整合时无法读取properties的处理方案

来源:互联网 发布:树莓派gpio编程工具 编辑:程序博客网 时间:2024/06/05 09:10

版本:Spring:3.1.4、Mybatis:3.1.1、Mybatis-Spring:1.1.1;

背景:

config.properties配置文件信息

Properties代码  收藏代码
  1. ##数据库(mysql|mssql|oracle...)  
  2. environment=mysql  
  3. jdbc.driver=com.mysql.jdbc.Driver  
  4. jdbc.url=jdbc:mysql://127.0.0.1:3306/portal?useUnicode=true&characterEncoding=utf8  
  5. jdbc.user=root  
  6. jdbc.password=root  
  7. #初始化连接(根据实际访问量设置大小)  
  8. jdbc.initialSize=10  
  9. #最大空闲连接(根据实际访问量设置大小)  
  10. jdbc.maxIdle=50  
  11. #最小空闲连接(根据实际访问量设置大小)  
  12. jdbc.minIdle=10  
  13. #最大连接数量(根据实际访问量设置大小)  
  14. jdbc.maxActive=200  

 Spring配置信息(截取部分)

Xml代码  收藏代码
  1. <context:property-placeholder location="classpath:config.properties" />  
  2.   
  3. <!--创建jdbc数据源 -->  
  4. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  5.     <property name="driverClassName" value="${jdbc.driver}"/>  
  6.     <property name="url" value="${jdbc.url}"/>  
  7.     <property name="username" value="${jdbc.user}"/>  
  8.     <property name="password" value="${jdbc.password}"/>  
  9.     <property name="initialSize" value="${jdbc.initialSize}"/>  
  10.     <property name="maxIdle" value="${jdbc.maxIdle}"/>  
  11.     <property name="minIdle" value="${jdbc.minIdle}"/>  
  12.     <property name="maxActive" value="${jdbc.maxActive}"/>  
  13.     <property name="removeAbandoned" value="true"/>    
  14.     <property name="removeAbandonedTimeout" value="120"/>  
  15.     <property name="maxWait" value="3000"/>  
  16. </bean>  
  17. <!-- 创建SqlSessionFactory,同时指定数据源 -->  
  18. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  19.     <property name="dataSource" ref="dataSource"/>  
  20.     <property name="configLocation" value="classpath:mybatis-config.xml"/>  
  21.        <property name="mapperLocations" value="classpath*:com.anly.portal.*.mapper/*Mapper.xml" />   
  22. </bean>  
  23. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  24.     <!-- 此处扫描的是Mapper接口 -->  
  25.     <property name="basePackage" value="com.anly.portal.*.mapper"/>  
  26.     <property name="sqlSessionFactory" ref="sqlSessionFactory"/>  
  27. </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
原创粉丝点击