Spring Boot 整合Mybatis非starter時,mapper一直沒法注入解決

原本呢,直接使用mybatis-spring-boot-starter仍是挺好的,可是咱們系統比較複雜,有多個數據源,其中一個平臺本身的數據源,另一些是動態配置出來的,二者徹底沒有關係。因此直接使用mybatis-spring-boot-starter就很麻煩了,會報下列錯誤:html

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found 2: dataSource,branchta at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1041) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090) at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.init(DataSourceInitializer.java:77) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
具體是init()中基於類型獲取dataSource的緣由: @PostConstruct
public void init() { if (!this.properties.isInitialize()) { logger.debug("Initialization disabled (not running DDL scripts)"); return; } if (this.applicationContext.getBeanNamesForType(DataSource.class, false, false).length > 0) { this.dataSource = this.applicationContext.getBean(DataSource.class); } if (this.dataSource == null) { logger.debug("No DataSource found so not initializing"); return; } runSchemaScripts(); }

 

就只能蛻回去使用mybatis-spring了。啓動的時候發現死活注入不進去,報下列錯誤:java

參考了http://www.cnblogs.com/insaneXs/p/9270071.html和https://blog.csdn.net/qq_21853607/article/details/72802080,經驗證並不是他們所述的問題。我debug的時候,發現mapper對象是有的,而不是第一個所述的沒有建立代理,最後debug的時候發現好像是druiddatasouce bean建立的時候出錯了,可是沒有堆棧信息。完整的配置以下:spring

application-bean.xml:sql

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <context:property-placeholder location="classpath*:jrescloud.properties" ignore-unresolvable="true" order="1"/>

    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${spring.datasource.driver-class-name}"/>
        <property name="url" value="${spring.datasource.url}"/>
        <property name="username" value="${spring.datasource.username}"/>
        <property name="password" value="${spring.datasource.password}"/>
        <!-- 配置初始化大小、最小、最大 -->
        <!-- <property name="initialSize" value="1"/> <property name="minIdle" value="100000"/> <property name="maxActive" value="10"/> -->
        <!-- 配置獲取鏈接等待超時的時間 -->
        <!-- <property name="maxWait" value="${jdbc.maxWait}"/> 打開PSCache,而且指定每一個鏈接上PSCache的大小 <property name="poolPreparedStatements" value="${jdbc.pps}"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="${jdbc.mpps}"/> 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/> 配置一個鏈接在池中最小生存的時間,單位是毫秒 <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/> <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/> <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/> <property name="logAbandoned" value="${jdbc.logAbandoned}"/> 配置監控統計攔截的filters <property name="filters" value="${jdbc.filters}"/> -->
    </bean>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 使用annotation 自動註冊bean, 並保證@Required、@Autowired的屬性被注入 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="${mybatis.configLocation}"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations">
            <list>
                <value>${mybatis.mapperLocations}</value>
            </list>
        </property>
    </bean>
</beans>
package com.XX.XXX.XXXX.config; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MybatisConfig { @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); mapperScannerConfigurer.setBasePackage("com.XX.XXX.XXXX.*.mapper"); return mapperScannerConfigurer; } }
package com.XX.XXX.XXXX.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource(locations={"classpath:application-bean.xml"}) public class AppConfig { @Bean public DynamicDataSourceRegister dynamicDataSourceRegister() { return new DynamicDataSourceRegister(); } @Bean public DynamicDataSourceConfig getDynamicDataSourceConfig() { return new DynamicDataSourceConfig(); } }
相關文章
相關標籤/搜索