前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >两种方案解决报错:Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasour

两种方案解决报错:Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasour

作者头像
猫头虎
发布2024-04-08 10:50:57
3.5K0
发布2024-04-08 10:50:57
举报
文章被收录于专栏:猫头虎博客专区

🌷🍁 博主猫头虎 带您 Go to New World.✨🍁 🦄 博客首页——猫头虎的博客🎐 🐳《面试题大全专栏》 文章图文并茂🦕生动形象🦖简单易学!欢迎大家来踩踩~🌺 🌊 《IDEA开发秘籍专栏》学会IDEA常用操作,工作效率翻倍~💐 🌊 《100天精通Golang(基础入门篇)》学会Golang语言,畅玩云原生,走遍大小厂~💐

🪁🍁 希望本文能够给您带来一定的帮助🌸文章粗浅,敬请批评指正!🍁🐥

两种方案解决报错:Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasour

方案一

在@SpringBootApplication后添加exclude = { DataSourceAutoConfiguration.class }来排除自动配置 :

代码语言:javascript
复制
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })

方案二

在application.yml文件中添加排除自动配置

代码语言:javascript
复制
spring:
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

application.properties

代码语言:javascript
复制
spring.autoconfigure.exclude= org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
代码语言:javascript
复制
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.2)

2022-09-28 17:03:06  [INFO]  org.springframework.boot.StartupInfoLogger:55  : Starting JoySpaceApplicationKt using Java 1.8.0_342 on DESKTOP-PFPD85S with PID 12140 (E:\Project-Code\OutsideApi\build\classes\kotlin\main started by DELL in E:\Project-Code\OutsideApi) 
2022-09-28 17:03:06  [INFO]  org.springframework.boot.SpringApplication:634  : No active profile set, falling back to 1 default profile: "default" 
2022-09-28 17:03:06  [INFO]  org.springframework.data.repository.config.RepositoryConfigurationDelegate:132  : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 
2022-09-28 17:03:06  [INFO]  org.springframework.data.repository.config.RepositoryConfigurationDelegate:201  : Finished Spring Data repository scanning in 8 ms. Found 0 JPA repository interfaces. 
2022-09-28 17:03:07  [INFO]  org.springframework.boot.web.embedded.tomcat.TomcatWebServer:108  : Tomcat initialized with port(s): 6060 (http) 
2022-09-28 17:03:07  [INFO]  org.apache.juli.logging.DirectJDKLog:173  : Initializing ProtocolHandler ["http-nio-6060"] 
2022-09-28 17:03:07  [INFO]  org.apache.juli.logging.DirectJDKLog:173  : Starting service [Tomcat] 
2022-09-28 17:03:07  [INFO]  org.apache.juli.logging.DirectJDKLog:173  : Starting Servlet engine: [Apache Tomcat/9.0.65] 
2022-09-28 17:03:07  [INFO]  org.apache.juli.logging.DirectJDKLog:173  : Initializing Spring embedded WebApplicationContext 
2022-09-28 17:03:07  [INFO]  org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext:292  : Root WebApplicationContext: initialization completed in 862 ms 
2022-09-28 17:03:07  [WARN]  org.springframework.context.support.AbstractApplicationContext:591  : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class 
2022-09-28 17:03:07  [INFO]  org.apache.juli.logging.DirectJDKLog:173  : Stopping service [Tomcat] 
2022-09-28 17:03:07  [INFO]  org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener:136  : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 
2022-09-28 17:03:07  [ERROR]  org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter:40  : 

***************************
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).
 

Process finished with exit code 1

兜底方案:

代码语言:javascript
复制
@SpringBootApplication(exclude = {
        DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class})

但是很多时候,加了这个注解,还是不能解决自动寻找配置文件中url进行初始化数据库连接的异常。 原因在于,在pom文件中,使用跟数据库相关的依赖,如spring-data,druid等,需要把数据库相关的依赖去掉,然后再加上注解,就能实现无数据库启动springboot了。

亲测方案2有效~

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-08-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 两种方案解决报错:Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasour
  • 方案一
  • 方案二
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档