springboot启动报错

前言

​ 学习构建spring boot+maven项目,在父模块下创建的一个新的模块,依赖版本由父模块pom.xml控制;写了一个测试接口,启动测试时,一直报错,初始化失败、datasource创建失败、没有合适的驱动,同时tomcat启动端口号也跟yml中不一致;

2023-01-09 23:23:37.192  WARN 15424 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class]: Invocation of init method failed; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2023-01-09 23:23:37.195  INFO 15424 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-01-09 23:23:37.210  INFO 15424 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-01-09 23:23:37.222 ERROR 15424 --- [           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 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).

排查

  • 数据库MySQL5.7.30
    • 驱动:com.mysql.jdbc.Driver
    • 连接地址、账号、密码无误
spring:
  # 配置mysql数据源
  datasource:
    druid:
      username: root
      password: root
      url: jdbc:mysql://localhost:3306/stock_db
      driver-class-name: com.mysql.jdbc.Driver
  • 检查spring各依赖版本一致

  • 修改子工程pom.xml打包方式为jar,还是报错

  • 网上查到,application类加注解,先把数据库连接信息过滤掉;

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
  • 父模块工程执行install,启动成功

之前只是重新install过子模块,但是pom.xml中添加了maven打包的插件spring-boot-maven-plugin,暂时还想不明白

posted @ 2023-01-10 14:41  CHEN_zu_he  阅读(233)  评论(0编辑  收藏  举报