spring+mybatis+druid无法读取jdbc.properties属性导致datasource加载失败

来源:互联网 发布:仓库存储储位优化 编辑:程序博客网 时间:2024/05/18 15:26

1.datasource.xml 相关配置信息


<?xml version="1.0" encoding="UTF-8"?>



<!-- Application context definition. -->
<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-4.0.xsd  
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd   
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
 ">


<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true" />
 
<!-- Mybatis session factory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
lazy-init="false">
<property name="mapperLocations" value="classpath:sqlmapper/*Mapper.xml" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com..dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>


    <!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
<property name="url" value="jdbc:mysql://localhost:3306/test"></property> 
<property name="username" value="root"></property> 
<property name="password" value="123456"></property>
</bean>--> 


<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
   <property name="driverClassName" value="${db.driver}"/> 
   <property name="url" value="${db.url}" /> 
   <property name="username" value="${db.user}" /> 
   <property name="password" value="${db.password}" /> 
    <!--<property name="connectionProperties" value="config.decrypt=true" />--> 
   <property name="initialSize" value="${db.pool.init}" /> 
   <property name="minIdle" value="${db.pool.minIdle}" /> 
   <property name="maxActive" value="${db.pool.maxActive}" />
   <property name="maxWait" value="60000" /> 
   <property name="validationQuery" value="SELECT 1 FROM DUAL" /> 
   <property name="testOnBorrow" value="false" /> 
   <property name="testOnReturn" value="false" /> 
   <property name="testWhileIdle" value="true" /> 
   <property name="timeBetweenEvictionRunsMillis" value="60000" /> 
   <property name="minEvictableIdleTimeMillis" value="25200000" /> 
   <property name="removeAbandoned" value="true" /> 
   <property name="removeAbandonedTimeout" value="1800" /> 
   <property name="logAbandoned" value="true" /> 
    <property name="filters" value="mergeStat,config" /> 
    </bean>



<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>


<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>


<bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean>
<!-- 事务(注解 )-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

</beans>


解决方案:

1.命令空间建议改成这样 版本用新的

2.  <!-- mapper代理配置-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <property name="basePackage" value="com.lexiang.main.mapper.orm" />
         <!-- 这里不能使用的配置  name=sqlSessionFactory 这样子会导致 数据源中spring注入项还未注入就初始化了数据源-->
          <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

0 0