所向无敌 发表于 2016-11-27 10:54:39

(转)spring3+mybatis 使用MapperScannerConfigurer时context:property-placeholder载不进属性

如题,使用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['driverClassName']}" />
      <property name="url" value="#{dataSourceProps['url']}" />
      <property name="username" value="#{dataSourceProps['username']}" />
      <property name="password" value="#{dataSourceProps['password']}" />
      <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.*"/>
页: [1]
查看完整版本: (转)spring3+mybatis 使用MapperScannerConfigurer时context:property-placeholder载不进属性