spring boot+mybatis 多数据源报错 No qualifying bean of type [javax.sql.DataSource] is defined:
2016-10-20 12:36
706 查看
spring boot 多数据源配置
No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: payment-dataSource,wacCommondataSource
之前一个数据源一切正常,今天突然增加一个数据源 如下两个数据源配置
配置的数据源是没有问题的,因为在另一个项目可以运行, 折腾了很久,最终通过跟踪源代码发现问题
原题在于项目依赖了 spring-boot-autoconfigure这个jar包
这个jar包中 有个DataSourceAutoConfiguration 会初始化DataSourceInitializer 这个类 ,这个类有一个init方法 会去获取DataSource(数据源)
初始化方法中 会获取数据源 需要初始化一些ddl操作 也是就runSchemaScripts()方法 检查初始化时是否需要执行sql script ,当你有两个数据源的时候,程序不知道取哪一个 ,所以报错
解决办法:
1.定义数据源的地方 加个primary="true" 记得只给其中的一个加, 当多数据源时 标示这个数据源是主要的
2. spring boot 启动类加上 exclude = DataSourceAutoConfiguration.class 代表启动项目的时候 不加载这个类
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})
相关文章推荐
- spring boot+mybatis 多数据源报错 No qualifying bean of type [javax.sql.DataSource] is defined:
- No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found
- Spring Boot + MyBatis No qualifying bean of type
- No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
- Spring Task Scheduler - No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
- Springboot RedisTemplate 报No qualifying bean of type... 不能按类型装配注入
- Spring两个数据源配置在容器启动出错,No unique bean of type [org.springframework.jdbc.core.JdbcTemplate] is defined:
- Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
- Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
- Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined(转)
- 解决:No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency
- Noqualifying bean of type [org.apache.solr.client.solrj.SolrServer] is defined:expected single match
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.itca
- Spring的JUnit错误:NoSuchBeanDefinitionException: No unique bean of type,expected single matching bean but found 2 [dataSource1,data
- junit 测试问题 No qualifying bean of type [javax.servlet.http.HttpServletRequest] found for dependency
- No qualifying bean of type [org.springframework.data.mongodb.core.MongoTemplate]
- 关于dao层Bean注入失败报No qualifying bean of type [xxx.xxx.xxx.xxx] is defined错误的问题
- No qualifying bean of type [] is defined
- No unique bean of type [javax.persistence.EntityManagerFactory] is defined
- spring-session整合redis集群的时候,No qualifying bean of type [org.springframework.session.SessionRepository