SpringBoot使用mybatis,发生:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured
最近,配置项目,使用SpringBoot2.2.1,配置mybatis访问db,配好后,使用自定义的数据源。启动发生:
APPLICATION FAILED TO START
*************************** Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class Action: Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
虽然,我知道,是因为我没有在application.yml文件中配置:spring.datasource。但是因为我是自定义的数据源,配置项不在spring.datasource,所以希望,不配置spring.datasource,也能启动项目。单纯在@SpringBootApplication(exclude = {DataSourceAutoConfigure.class})中排除DataSourceAutoConfigure是不行的。
下面是解决方案:
方案1:
在自定义数据源中,添加自定义自动配置类。见下面的例子:
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Properties; /**
* 自定义自动配置类
* 1. 自定义数据源,自定义数据源是为了,防止因为没有在配置文件中,配置spring.datasource,
* 出现:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
* 2. 切面类,用来切换主从数据源
*
*/
@EnableAspectJAutoProxy
@Configuration
public class CustomDataSourceAutoConfiguration { @Bean
@ConditionalOnMissingBean
public DataSource defaultDataSource() {
return new CustomDataSource());
}
} @Bean
@ConditionalOnBean({DataSource.class})
public CustomDataSourceAspect CustomDsAspect() {
return new CustomDataSourceAspect();
}
}
别忘了,在META-INF/spring.factories文件中配置:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xxx.framework.datasource.masterslave.CustomDataSourceAutoConfiguration
方案2:
在启动类上,添加如下代码:
import com.lzj.framework.datasource.multi.MultiDatabaseMapperFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan(basePackages = {"com.xxx.test.dao"}, factoryBean = MultiDatabaseMapperFactoryBean.class)
public class LzjFrameworkTestApplication { public static void main(String[] args) {
SpringApplication.run(LzjFrameworkTestApplication.class, args);
} }
第一要在@SpringBootApplication中排除DataSourceAutoConfiguration类的配置;第二,要配置@MapperScan,在参数中指出,扫描标准@Mapper注解的dao接口的包位置,和自定义factoryBean的实现类。factoryBean需要自己实现,需要继承MapperFactoryBean接口。
当然,你要是不使用数据库,只需把DataSourceAutoConfiguration排除就可以了。
项目,就可以正常启动了。
SpringBoot使用mybatis,发生:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured的更多相关文章
- springboot启动报错 Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
新建了一个springboot项目报一下错误: Failed to configure a DataSource: 'url' attribute is not specified and no em ...
- 新建springboot项目启动出错 Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
错误信息入下: 2018-06-23 01:48:05.275 INFO 7104 --- [ main] o.apache.catalina.core.StandardService : Stopp ...
- 记一次Spring boot集成mybatis错误修复过程 Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
最近自己写了一份代码签入到github,然后拉下来运行报下面的错误 Error starting ApplicationContext. To display the conditions repor ...
- Spring Boot错误——SpringBoot 2.0 报错: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
背景 使用Spring Cloud搭建微服务,服务的注册与发现(Eureka)项目启动时报错,错误如下 *************************** APPLICATION FAILED T ...
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class 消费者提示需要配置数据源
使用 由于给前端做分页 在启动消费者的时候遇到了这个问题 Failed to configure a DataSource: 'url' attribute is not specified and ...
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
SpringBoot项目编译成功,启动报错 提示信息很明显,通过查看依赖关系,可以找到原因 导致这个问题的原因是因为,在 pom.xml 配置文件中,配置了数据连接技术 spring-boot-sta ...
- 关于“Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.”
Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the c ...
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.Reason: Failed to determine a suitable driver class
解决方案: @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) 作用://取消数据库配置 但是 在用到数据库的时候记 ...
- 奇葩的Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
启动springboot的时候莫名其妙出现这个错误,我properties里面也没配置数据源啥的,但就是出现这个错误 解决方法: 在启动类上加@SpringBootApplication(exclud ...
随机推荐
- VS2017 远程调试linux(centos).net core程序(通过附加程序的方式)
参考两位大神的帖子: https://blog.csdn.net/soband_xiang/article/details/82914195 https://blog.csdn.net/weixin_ ...
- 第09组 Alpha冲刺(2/6)
队名:观光队 组长博客 作业博客 组员实践情况 王耀鑫 过去两天完成了哪些任务 文字/口头描述 完成服务器连接数据库部分代码 展示GitHub当日代码/文档签入记录 接下来的计划 服务器网络请求. 还 ...
- IntelliJ IDEA 2019从入门到癫狂 图文教程!
阅读本文大概需要 6 分钟. 作者:yizhiwazi 来源:www.jianshu.com/p/9c65b7613c30 前言:IntelliJ IDEA 如果说IntelliJ IDEA是一款现代 ...
- 干货收藏 | Java 程序员必备的一些流程图
阅读本文大概需要 6 分钟. 转载自:https://juejin.im/post/5d214639e51d4550bf1ae8df 1.Spring 的生命周期 Spring 作为当前 Java 最 ...
- DataGrip:Error encountered when performing Introspect schema xxx 错误的解决方法
datagrip的问题,转载自: https://www.cnblogs.com/geb515/p/7995249.html 把Introspect using JDBC _metadata打上勾 然 ...
- [技术博客] win10下vagrant+centos7 rails虚拟开发机配置流程
由于少昂早年已经在此踩过坑了,因此在这里,我们现在直接贴上他早年的博客链接:https://www.cnblogs.com/HansBug/p/7403306.html
- [技术博客]采用Qthread实现多线程连接等待
采用Qthread实现多线程连接等待 本组的安卓自动化测试软件中,在测试开始前需要进行连接设备的操作,如下图左侧的按钮 后端MonkeyRunner相关操作的程序中提供了connect() ...
- 关于用maven创建的springboot工程打包jar后找不到配置文件的问题
你的resources文件夹的名称写错了!!! 如果不在pom中配置build的资源路径,那么你的资源文件名必须默认是“resources”! 解决办法有两个: 1.修改文件夹名称 2.添加pom的b ...
- How to Use Convolutional Neural Networks for Time Series Classification
How to Use Convolutional Neural Networks for Time Series Classification 2019-10-08 12:09:35 This blo ...
- python mysql数据库压力测试
python mysql数据库压力测试 pymysql 的执行时间对比 1,装饰器,计算插入1000条数据需要的时间 def timer(func): def decor(*args): start_ ...