spring boot+mybatis 多数据源报错 No qualifying bean of type [javax.sql.DataSource] is defined:

文章转载于http://lib.csdn.net/article/java/65575?knId=1400

spring boot 多数据源配置   


 No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: payment-dataSource,wacCommondataSource 


之前一个数据源一切正常,今天突然增加一个数据源  如下两个数据源配置

<bean id="wacCommondataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
    <property name="url" value="${common.database.jdbc.url}" />
    <property name="username" value="${common.database.jdbc.username}" />
    <property name="password" value="${common.database.jdbc.password}" />
    <property name="connectionProperties" value="config.decrypt=true" />
    <property name="filters">
        <value>stat,config</value>
    </property>

    <property name="maxActive">
        <value>20</value>
    </property>
    <property name="initialSize" value="20" />
    <property name="maxWait">
        <value>60000</value>
    </property>
    <property name="minIdle">
        <value>1</value>
    </property>
    <property name="timeBetweenEvictionRunsMillis">
        <value>60000</value>
    </property>
    <property name="minEvictableIdleTimeMillis">
        <value>300000</value>
    </property>
    <property name="validationQuery">
        <value>SELECT 'x'</value>
    </property>
    <property name="testWhileIdle">
        <value>true</value>
    </property>
    <property name="testOnBorrow">
        <value>false</value>
    </property>
    <property name="testOnReturn">
        <value>false</value>
    </property>
    <property name="poolPreparedStatements">
        <value>false</value>
    </property>
    <property name="maxOpenPreparedStatements">
        <value>20</value>
    </property>
    <property name="proxyFilters">
        <list>
            <ref bean="stat-filter" />
<!--    <ref bean="wacDruid-filter" /> <!– 引入拦截 –>-->
</list>
    </property>

</bean>

<!-- Mybatis session factory -->
<bean id="wacCommonSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="wacCommondataSource" />
    <property name="mapperLocations" value="classpath:sqlmapper/common/*.xml  " />
    <property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>


<!--mapper映射接口包扫描路径 -->
<bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.wacai.payment.dal.mapper.common" />
    <property name="sqlSessionFactoryBeanName" value="wacCommonSqlSessionFactory" />
</bean>

<bean id="payment-dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close" >
    <property name="url" value="${thea.database.jdbc.url}" />
    <property name="username" value="${thea.database.jdbc.username}" />
    <property name="password" value="${thea.database.jdbc.password}" />
    <property name="connectionProperties" value="config.decrypt=true" />
    <property name="filters">
        <value>stat,config</value>
    </property>

    <property name="maxActive">
        <value>20</value>
    </property>
    <property name="initialSize" value="20" />
    <property name="maxWait">
        <value>60000</value>
    </property>
    <property name="minIdle">
        <value>1</value>
    </property>
    <property name="timeBetweenEvictionRunsMillis">
        <value>60000</value>
    </property>
    <property name="minEvictableIdleTimeMillis">
        <value>300000</value>
    </property>
    <property name="validationQuery">
        <value>SELECT 'x'</value>
    </property>
    <property name="testWhileIdle">
        <value>true</value>
    </property>
    <property name="testOnBorrow">
        <value>false</value>
    </property>
    <property name="testOnReturn">
        <value>false</value>
    </property>
    <property name="poolPreparedStatements">
        <value>false</value>
    </property>
    <property name="maxOpenPreparedStatements">
        <value>20</value>
    </property>
    <property name="proxyFilters">
        <list>
            <ref bean="stat-filter" />
<!--    <ref bean="wacDruid-filter" /> <!– 引入拦截 –>-->
</list>
    </property>

</bean>



<!--<bean id="wacDruid-filter" class="com.wacai.pt.druid.masking.process.filter.WacDruidLogFilter">
    <property name="logEnable" value="false" /> <!– 开启输出 –>
</bean>-->

<!-- Mybatis session factory -->
<bean id="payment-theaSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
lazy-init="false">
    <property name="dataSource" ref="payment-dataSource" />
    <property name="mapperLocations" value="classpath:sqlmapper/limbic/*.xml" />
    <property name="configLocation" value="classpath:mybatis-config.xml" />
    <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageHelper">
                <property name="properties">
                    <value>
dialect=mysql
</value>
                </property>
            </bean>
        </array>
    </property>
</bean>

<!--mapper映射接口包扫描路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.wacai.payment.dal.mapper.limbic" />
    <property name="sqlSessionFactoryBeanName" value="payment-theaSqlSessionFactory" />
</bean>

配置的数据源是没有问题的,因为在另一个项目可以运行, 折腾了很久,最终通过跟踪源代码发现问题 
原题在于项目依赖了 spring-boot-autoconfigure这个jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

这个jar包中 有个DataSourceAutoConfiguration  会初始化DataSourceInitializer 这个类 ,这个类有一个init方法 会去获取DataSource(数据源)

@Configuration
@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
@EnableConfigurationProperties(DataSourceProperties.class)
@Import({ Registrar.class, DataSourcePoolMetadataProvidersConfiguration.class })
public class DataSourceAutoConfiguration {

@Configuration
@ConditionalOnMissingBean(DataSourceInitializer.class)
protected static class DataSourceInitializerConfiguration {

@Bean
public DataSourceInitializer dataSourceInitializer() {
return new DataSourceInitializer();
}

}
初始化方法中 会获取数据源 需要初始化一些ddl操作 也是就runSchemaScripts()方法 检查初始化时是否需要执行sql script ,当你有两个数据源的时候,程序不知道取哪一个 ,所以报错

@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();
}

private void runSchemaScripts() {
List<Resource> scripts = getScripts(this.properties.getSchema(), "schema");
if (!scripts.isEmpty()) {
runScripts(scripts);
try {
this.applicationContext.publishEvent(new DataSourceInitializedEvent(
this.dataSource));
// The listener might not be registered yet, so don't rely on it.
if (!this.initialized) {
runDataScripts();
this.initialized = true;
}
}
catch (IllegalStateException ex) {
logger.warn("Could not send event to complete DataSource initialization ("
+ ex.getMessage() + ")");
}
}
}

解决办法: 
1.定义数据源的地方 加个primary="true" 记得只给其中的一个加, 当多数据源时 标示这个数据源是主要的
<bean id="payment-dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close" primary="true">
2. spring boot 启动类加上 exclude = DataSourceAutoConfiguration.class  代表启动项目的时候 不加载这个类
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值