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

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})
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值