报错Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource coul

编程入门 行业动态 更新时间:2024-10-19 21:39:32

<a href=https://www.elefans.com/category/jswz/34/1771188.html style=报错Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource coul"/>

报错Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource coul

今天在使用微服务eureka的时候遇到这样一个错误,看报错的大概意思就是没有配置数据源。

原因:

看报错,就是字面意思,没能成功的配置数据源:url这个参数应该被配置

其实简单来说就是我集成了mybatis,但是我在配置eureka服务的时候并不会使用mybatis,即我没有配置mybatis必须要配置的东西。但是我在eureka模块的pom中并没有引入mybatis,最后才发现是pom继承了父类的pom,父类pom中引入了mybatis,并且有具体的版本号。

spring boot 会默认加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration这个类,

而DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean,又因为项目(oss模块)中并没有关于dataSource相关的配置信息,所以当spring创建dataSource bean时因缺少相关的信息就会报错。

解决方案:

        一:简单粗暴把mybatis注释掉就可以了,不过我们写入父类就是为了方便子类继承他。所以第二种方案。

        二:在@SpringBootApplication启动类注解上加上exclude,解除自动加载DataSourceAutoConfiguration

package cn.itcase.eureka;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloudflix.eureka.server.EnableEurekaServer;@EnableEurekaServer
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class EurekaApplication {public static void main(String[] args){SpringApplication.run(EurekaApplication.class,args);}
}

        三:第三种解决方案也是我觉得不错的方案。如图,父类pom的mybatis依赖本来是有具体的版本号,我们把版本号改成变量即可。子类使用时也不需要写版本号了。

 

 

 

 

 

 

更多推荐

报错Failed to configure a DataSource: ‘url‘ attribute is not specified and no embe

本文发布于:2024-03-12 11:39:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1731423.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:报错   configure   DataSource   url   Failed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!