---
title: "(转)spring3+mybatis 使用MapperScannerConfigurer时context:property-placeholder载不进属性"
author: "lean1252"
date: "2014-12-30T18:34:42Z"
tags: []
categories: ["编程语言"]
slug: "spring3-mybatis-mapperscannerconfigurer-context-property-placeholder"
draft: false
lastmod: "2014-12-30T18:34:42Z"
id: 2171291
url: "https://asflex.iteye.com/blog/2171291"
---

如题，使用spring3+mybatis时，使用mybatis官方提供的动态加载自动注入Mapper方法时，写在配置文件里的数据源信息加载滞后，导致加载失败。这个问题在：http://www.oschina.net/question/188964_32305有比较多的讨论，但我仔细测试过，都不能达到我想的结果，这里我参考了mybatis的官方论坛：http://code.google.com/p/mybatis/issues/detail?id=414第17楼的想法，做到了自动注入Mapper到Spring容器中，自动加载sqlMapper的XML文件。
下面贴出我的配置：
 
Xml代码  收藏代码
<util:properties id="dataSourceProps" location="classpath:resources/config/jdbc.properties"/>  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
        <property name="driverClassName" value="#{dataSourceProps}" />  
        <property name="url" value="#{dataSourceProps}" />  
        <property name="username" value="#{dataSourceProps}" />  
        <property name="password" value="#{dataSourceProps}" />  
        <property name="initialSize" value="1" />  
        <property name="maxActive" value="500" />  
        <property name="maxIdle" value="2" />  
        <property name="minIdle" value="1" />  
    </bean>  
  
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
  
    <tx:annotation-driven />    
      
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    
         <property name="dataSource" ref="dataSource" />    
         <property name="typeAliasesPackage" value="com.tydic.*.model,com.tydic.*.*.model" />  
    </bean>   
        <!--Mapper与sqlMap的xml文件要存一起，否则要在sqlSessionFactory里扫描加载-->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    
          <property name="basePackage" value="com.tydic.*.dao,com.tydic.*.*.dao" />  
    </bean>  
   <context:component-scan base-package="com.tydic.*"/>