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

  方案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

原文地址:https://www.cnblogs.com/feiyujun/p/11964310.html

时间: 2024-11-08 07:02:42

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.

报错内容具体如下 *************************** 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

springboot 新工程报错 Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

错误日志 ... ... Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2020-04-02 12:52:47.306 ERROR 22036 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLIC

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

springboot报错截图: 报错原因: 使用多数据源,第一次运行前要install 解决方法: 没错,就是这个鬼东西: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured 原文地址:https://www.cnblogs.com/biaogejiushibiao/p/9926214.html

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

启动springboot的时候莫名其妙出现这个错误,我properties里面也没配置数据源啥的,但就是出现这个错误 解决方法: 在启动类上加@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) 即在@SpringBootApplication上加(exclude= {DataSourceAutoConfiguration.class}) 其他因为数据源的问题出现的这个错误网上好多,这里不赘述. 奇葩的Faile

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) 作用://取消数据库配置 但是 在用到数据库的时候记得将他改为 @SpringBootApplication , 否则会报错:如下org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'xxController':

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

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 Description: Failed to configure a DataSource: 'url' attribute is not

Spring Boot 2.1.7 启动项目失败,报错: "Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured."

一开始按照网上的很多解决办法是: 启动类头部声明@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}),但是这样会排除掉我自己的数据库配置, 检查了很多地方都没发现问题在哪里,最后自己猜想不论怎么修改application.yml文件,都不生效,是不是因为这个配置文件就没有被加载到? 带着这个想法我就去网上搜了一下问题,有个问题看到了target目录的作用,就去想了一下是不是application.yml文件没有被

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

有的时候,dubbo的消费者不需要配置数据源: 在子项目引入父项目依赖的时候 同时引入了自动配置数据源的依赖,有没有配置数据源的时候,就会报错: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. 两种解决方法: 如果不需要配置数据源,又需要使用依赖,可以使用第一种方法 取消数据源的自动配置: @SpringBootAp

springboot项目启动报错 url' attribute is not specified and no embedded datasource could be configured

报错相关信息: 2019-07-22 17:12:48.971 ERROR 8312 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not