报错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
发布评论