spring-boot启动提示Failed to determine suitable jdbc url

今天在启动一个测试项目的时候,突然报如下错误:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2019-04-21 01:20:02.376  INFO 14001 --- [           main] o.j.l.example.LoggingExampleApplication  : Starting LoggingExampleApplication on jiangliongdembp with PID 14001 (/Users/jiangliuhong/develop/cvs/gitee/logging/java/logging-example/target/classes started by jiangliuhong in /Users/jiangliuhong/develop/cvs/gitee/logging/java)
2019-04-21 01:20:02.380  INFO 14001 --- [           main] o.j.l.example.LoggingExampleApplication  : No active profile set, falling back to default profiles: default
2019-04-21 01:20:03.374  INFO 14001 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-04-21 01:20:03.391  INFO 14001 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-04-21 01:20:03.391  INFO 14001 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.17]
2019-04-21 01:20:03.455  INFO 14001 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-04-21 01:20:03.455  INFO 14001 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1040 ms
2019-04-21 01:20:03.531  WARN 14001 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testApi': Unsatisfied dependency expressed through field 'studentService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentDao' defined in file [/Users/jiangliuhong/develop/cvs/gitee/logging/java/logging-example/target/classes/org/jarome/logging/example/dao/StudentDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' 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 suitable jdbc url
2019-04-21 01:20:03.533  INFO 14001 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-04-21 01:20:03.545  INFO 14001 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-04-21 01:20:03.549 ERROR 14001 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
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 suitable jdbc url


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

看见这个的第一反应是配置写错了。其实不然。检查配置后,发现是url没有被读取到,配置文件没有生效,其大概原因有以下几点:

  • 系统编码不正确。跑项目之前最好让项目与文件的编码对应起来
  • 可能有和你所在项目平级的配置文件,springboot会优先读取项目同级别目录下的配置文件
  • idea没有将resource设置为资源目录

通过排查后发现,我的问题属于没有设置为资源目录。

在这里插入图片描述

选中resources目录,点击idea的Resource按钮,将其设置为资源目录后,项目正常启动。仅以此贴告诫自己。

对于此类情况,也有可能是你应用了mybatis相关的starter,但是你的项目又没有使用,所以就没有配置datasource相关的配置项,所以导致url找不到,这类情况的话,要不把引用移除,要不就禁止datasource的自动装配。

禁止datasource自动装配办法:

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
GitHub 加速计划 / sp / spring-boot
73.97 K
40.4 K
下载
spring-projects/spring-boot: 是一个用于简化Spring应用开发的框架。适合用于需要快速开发企业级Java应用的项目。特点是可以提供自动配置、独立运行和内置的Tomcat服务器,简化Spring应用的构建和部署。
最近提交(Master分支:2 个月前 )
fdf24c6c Closes gh-42976 10 天前
3c42ba8c * pr/42974: Fix copyright year of updated file Polish Closes gh-42974 10 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐