最近,配置项目,使用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的更多相关文章

  1. 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 ...

  2. 新建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 ...

  3. 记一次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 ...

  4. 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 ...

  5. 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 ...

  6. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

    SpringBoot项目编译成功,启动报错 提示信息很明显,通过查看依赖关系,可以找到原因 导致这个问题的原因是因为,在 pom.xml 配置文件中,配置了数据连接技术 spring-boot-sta ...

  7. 关于“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 ...

  8. 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) 作用://取消数据库配置 但是 在用到数据库的时候记 ...

  9. 奇葩的Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

    启动springboot的时候莫名其妙出现这个错误,我properties里面也没配置数据源啥的,但就是出现这个错误 解决方法: 在启动类上加@SpringBootApplication(exclud ...

随机推荐

  1. GoCN每日新闻(2019-11-05)

    GoCN每日新闻(2019-11-05) GoCN每日新闻(2019-11-05) 1. Protobuf 终极教程 https://colobu.com/2019/10/03/protobuf-ul ...

  2. Centos7 .net core 2.0安装使用

    一.添加dotnet产品Feed sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc sudo sh -c 'ech ...

  3. JS字符串转换为JSON的四种方法

    转自:https://www.cnblogs.com/hgmyz/p/7451461.html 1.jQuery插件支持的转换方式:  示例: $.parseJSON( jsonstr ); //jQ ...

  4. [Beta]Scrum Meeting#1

    github 本次会议项目由PM召开,时间为5月6日晚上10点30分 时长15分钟 任务表格 人员 昨日工作 下一步工作 木鬼 beta初步计划 撰写博客整理文档 swoip 前端改进计划 模块松耦合 ...

  5. CT窗宽位宽

    先说一下CT值是什么 CT图像反映的是人体对X射线吸收的系数,但我们关心的是各组织结构的密度差异,即相对密度,如果某组织发生病变,其密度就会发生变化,但由于比较吸收系数非常繁琐,于是亨氏把组织器官对X ...

  6. Mesa: GeoReplicated, Near RealTime, Scalable Data Warehousing

    Mesa的定义并没有反映出他的特点,因为分布式,副本,高可用,他都是依赖google的其他基础设施完成的 他最大的特点是,和传统数仓比,可以做到near real-time的返回聚合的查询结果 算入实 ...

  7. Windows Server 2012 R2 卸载IE浏览器

    If you run any Windows Servers, you may run into a scenario where you want to remove access to Inter ...

  8. SoapUI: 设置case的属性变量

    琐碎的东西也想一点一滴的记下来

  9. HSBImageView--android--可以设置HSB值的imageview

    package guide.yunji.com.guide.view; import android.content.Context; import android.content.res.Typed ...

  10. eclipse从git下载的maven项目需要转成maven才可是使用main方法启动

    导入git项目: 选择导入git项目有会有两个选项:一个是从本地git仓库中导入项目,一个是从github远程仓库中导入项目 我们选择从远程仓库中导入项目: 然后选择本地存放该项目的git仓库 然后选 ...