• Spring Boot 整合Mybatis非starter时,mapper一直无法注入解决


    本来呢,直接使用mybatis-spring-boot-starter还是挺好的,但是我们系统比较复杂,有多个数据源,其中一个平台自己的数据源,另外一些是动态配置出来的,两者完全没有关系。所以直接使用mybatis-spring-boot-starter就很麻烦了,会报下列错误:

    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了。启动的时候发现死活注入不进去,报下列错误:

    参考了http://www.cnblogs.com/insaneXs/p/9270071.html和https://blog.csdn.net/qq_21853607/article/details/72802080,经验证并非他们所述的问题。我debug的时候,发现mapper对象是有的,而不是第一个所述的没有创建代理,最后debug的时候发现好像是druiddatasouce bean创建的时候出错了,但是没有堆栈信息。完整的配置如下:

    application-bean.xml:

    <?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();
        }
    }
  • 相关阅读:
    General part中方向选取的作用
    mount part中位置的作用
    关于zero pivot
    Revit二次开发示例:ModelessForm_ExternalEvent
    elasticsearch6.4 memory locking requested for elasticsearch process but memory is not locked
    百度网盘 http://pandownload.com/index.html
    MySQL 5.7主从复制从零开始设置及全面详解——实现多线程并行同步,解决主从复制延迟问题!
    linux 系统优化
    服务器cpu过高修复:操作系统内核bug导致
    Jvm中时区设置方式
  • 原文地址:https://www.cnblogs.com/zhjh256/p/9948517.html
Copyright © 2020-2023  润新知