您的位置:首页 > 编程语言 > Java开发

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

2018-02-26 23:03 375 查看
版本:Spring:3.1.4、Mybatis:3.1.1、Mybatis-Spring:1.1.1;背景:config.properties配置文件信息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配置信息(截取部分)Xml代码  

<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。

一个完成的配置示例:[html] view plain copy<?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.xsd  
    http://www.springframework.org/schema/aop   
    http://www.springframework.org/schema/aop/spring-aop-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">  
  
  
     <!-- 属性文件 -->   
    <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>  
版本:Spring:3.1.4、Mybatis:3.1.1、Mybatis-Spring:1.1.1;背景:config.properties配置文件信息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配置信息(截取部分)Xml代码  

<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。

一个完成的配置示例:[html] view plain copy<?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.xsd  
    http://www.springframework.org/schema/aop   
    http://www.springframework.org/schema/aop/spring-aop-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">  
  
  
     <!-- 属性文件 -->   
    <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>  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: