java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot启动失败

SpringBoot启动失败的原因及其解决方法

作者:冲锋的禾

对于springboot的启动失败,相信大家都有经历,但是为什么会启动失败,以及怎么解决都只能通过日志进行查看,在这里,我会将常见的springboot启动失败的报错一一展示,需要的朋友可以参考下

前言:

对于springboot的启动失败,相信大家都有经历,但是为什么会启动失败,以及怎么解决都只能通过日志进行查看,在这里,我会将常见的springboot启动失败的报错一一展示

报错:

1:端口占用报错

***************************
APPLICATION FAILED TO START
***************************
 
Description:
The Tomcat connector configured to listen on port 8987 failed to start. The port  may already be in use or the connector may be misconfigured.
 
// 配置为监听端口8987的Tomcat连接器启动失败。端口可能已经在使用中,或者连接器可能配置错误。
 
Action:
Verify the connector's configuration, identify and stop any process that's liste ning on port 8987, or configure this application to listen on another port.
 
// 验证连接器的配置,识别并停止端口8987上正在运行的任何进程,或将此应用程序配置为监听另一个端口。
  • 报错:The port may already be in use  —>  端口可能已经在使用中
  • 原因:8987 端口被占用
  • 解决:给springboot换一个端口或关闭占用端口的程序。

2:mapper扫描类报错

***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
Field clustersDetailsAmqMapper in com.ruoyi.clusters.service.impl.ClustersDetailsAmqServiceImpl required a bean of type 'com.ruoyi.clusters.mapper.ClustersDetailsAmqMapper' that could not be found.
 
// 在 com.ruoyi.clusters.service.impl.ClustersDetailsAmqServiceImpl 中的属性 clustersDetailsAmqMapper 需要一个类型为'com.ruoyi.clusters.mapper.ClustersDetailsAmqMapper'的bean,但是找不到它。
 
The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)
 
// 注入点具有以下注释:-@org.springframework.beans.factory.annotation.Autowired(必需=true)
 
Action:
 
Consider defining a bean of type 'com.ruoyi.clusters.mapper.ClustersDetailsAmqMapper' in your configuration.
 
// 考虑在配置中定义一个类型为'com.ruoyi.clusters.mapper.ClustersDetailsAmqMapper'的bean。
  • 报错:Field xxxMapper in xxx required a bean of type 'xxxMapper' that could not be found. —> 在xxx中的属性xxxMapper 需要一个类型为'xxxMapper'的 bean,但是找不到它。
  • 原因:springboot的启动类 没有配置mapper扫描路径 或 配置的mapper文件扫描路径不对。
  • 解决:在springboot启动类中 配置正确的mapper文件扫描,如下:
@SpringBootApplication
@MapperScan(value = "com.ruoyi.**.mapper")  
// 路径必须与mapper文件在的路径一致,否则依然会报错
public class RuoYiApplication {
    public static void main(String[] args) {
        SpringApplication.run(RuoYiApplication.class, args);
    }
}

注意:* 指的是下一级目录,** 指的是包含其子目录在内的。

如果Mapper文件在com.ruoyi.testa.mapper中,用@MapperScan(value = "com.ruoyi.*.mapper") 是可以的,

如果文件是在com.ruoyi.testa.mytest.mapper中,1个 * 配置依然会找不到这个Mapper文件,会报同样的错误,应该用**,@MapperScan(value = "com.ruoyi.**.mapper")。
 

3.数据库连接问题

***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
// 配置数据源失败:没有指定'url'属性,无法配置嵌入的数据源。
 
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).
// 请考虑以下几点:
// 如果你想要嵌入式数据库(H2、HSQL或Derby),请把它放在classpath(类路径)上。
// 如果要从特定配置文件加载数据库设置,则可能需要激活它(当前没有配置文件处于活动状态)。

报错:Failed to determine a suitable driver class  —> 没法确定合适的驱动程序类

原因:没有在application.yml中配置数据源(比如druid连接池)或 数据库驱动(比如mysql)

加上数据源配置:

# 数据源配置
spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: com.mysql.cj.jdbc.Driver
        druid:
            # 主库数据源
            master:
                url: jdbc:mysql://127.0.0.1:3306/car?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
                username: root
                password: root

加上数据库坐标:

 <!-- Mysql驱动包 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

4:没有绑定连接池

***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
Failed to bind properties under 'spring.datasource.type' to java.lang.Class<javax.sql.DataSource>:
// 没法把'spring.datasource.type'下的属性绑定到java.lang.Class<javax.sql.datasource>:
    Property: spring.datasource.type
    // 属性:spring.datasource.type
    Value: com.alibaba.druid.pool.DruidDataSource
    // 值: com.alibaba.druid.pool.DruidDataSource
    Origin: class path resource [application-druid.yml]:4:15
    //来源: 类路径资源[application-druid.yml]:4:15
    Reason: No converter found capable of converting from type [java.lang.String] to type [java.lang.Class<javax.sql.DataSource>]
// 原因:找不到能够从类型[java.lang.String]转换为类型[java.lang.Class<javax.sql.DataSource>]的转换器
 
Action:
 
Update your application's configuration
// 更新应用程序的配置

原因:没导入Druid连接池的坐标,所以找不到com.alibaba.druid.pool.DruidDataSource,把Druid连接池的坐标正确导入即可

<!--阿里数据库连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
</dependency>

5:无法确定合适的jdbc url

***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
// 配置数据源失败:没有指定“url”属性,无法配置嵌入的数据源。
 
Reason: Failed to determine suitable jdbc url
// 原因:无法确定合适的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).
// 请考虑以下几点:
// 如果你想要嵌入式数据库(H2、HSQL或Derby),请把它放在类路径上。
// 如果要从特定配置文件加载数据库设置,则可能需要激活它(当前没有激活的配置文件)。
报错:Failed to determine suitable jdbc url  —> 无法确定合适的jdbc url
原因:

url没有被读取到,配置文件没有生效,其大概原因有以下几点:

系统编码不正确。跑项目之前最好让项目与文件的编码对应起来
可能有和你所在项目平级的配置文件,springboot会优先读取项目同级别目录下的配置文件
idea没有将resource设置为资源目录
检查DruidConfig配置类(或配置文件)
检查一下Application启动文件的位置,建议将其放在外层:如果SpringBootApplication启动文件位置在数据库配置DruidConfig文件下层,则不会对这个Configuration进行扫描,然后去找默认的配置,所以导致Failed to determine suitable jdbc url

6:自定义数据源一定要排除SpringBoot自动配置数据源,不然会出现循环引用的问题

Description:
 
The dependencies of some of the beans in the application context form a cycle:
// 应用程序上下文中某些bean的依赖关系形成一个循环:
 
   testAController (field private com.ruoyi.testa.service.TestAService com.ruoyi.apis.TestAController.testAService)
      ↓
   testAServiceImpl (field private com.ruoyi.testa.mapper.TestAMapper com.ruoyi.testa.service.impl.TestAServiceImpl.testAMapper)
      ↓
   testAMapper defined in file [E:\IdeaProjects2018\car\ruoyi-mapper\target\classes\com\ruoyi\testa\mapper\TestAMapper.class]
      ↓
   sqlSessionFactory defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]
┌─────┐
|  dynamicDataSource defined in class path resource [com/ruoyi/config/DruidConfig.class]
↑     ↓
|  masterDataSource defined in class path resource [com/ruoyi/config/DruidConfig.class]
↑     ↓
|  org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker
└─────┘
  • 异常出现场景:SpringBoot中自定义DataSource数据源
  • 原因:自定义数据源一定要排除SpringBoot自动配置数据源,不然会出现循环引用的问题。
  • 解决:排除Spring Boot数据源自动配置类
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@MapperScan(value = "com.ruoyi.**.mapper") 
public class RuoYiApplication {
    public static void main(String[] args) {
        SpringApplication.run(RuoYiApplication.class, args);
    }
}

7:由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.3.RELEASE)
2020-09-01 11:21:54.103  INFO 21308 --- [           main] com.smr.bluetooth.DeviceCommApplication  : Starting DeviceCommApplication on LAPTOP-RM7CN477 with PID 21308 (E:\IdeaProjects2018\bluetooth_test\target\classes started by 且随疾风前行 in E:\IdeaProjects2018\bluetooth_test)
2020-09-01 11:21:54.106  INFO 21308 --- [           main] com.smr.bluetooth.DeviceCommApplication  : No active profile set, falling back to default profiles: default
2020-09-01 11:21:54.133  INFO 21308 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@770c2e6b: startup date [Tue Sep 01 11:21:54 CST 2020]; root of context hierarchy
2020-09-01 11:21:54.232  WARN 21308 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
2020-09-01 11:21:54.605 ERROR 21308 --- [           main] o.s.boot.SpringApplication               : Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
	at com.smr.bluetooth.DeviceCommApplication.main(DeviceCommApplication.java:10) [classes/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
	... 8 common frames omitted

报错:Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean. (由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext)

解决:启动类添加@EnableAutoConfiguration注解即可解决问题

8:依赖冲突

***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
An attempt was made to call a method that does not exist. The attempt was made from the following location:
 
org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1319)
 
The following method did not exist:
 
javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
 
The calling method's class, org.apache.catalina.authenticator.AuthenticatorBase, was loaded from the following location:
jar:file:/E:/Maven/maven-repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.63/tomcat-embed-core-9.0.63.jar!/org/apache/catalina/authenticator/AuthenticatorBase.class
The called method's class, javax.servlet.ServletContext, is available from the following locations:
 
jar:file:/E:/Maven/maven-repository/org/apache/tomcat/servlet-api/6.0.18/servlet-api-6.0.18.jar!/javax/servlet/ServletContext.class
jar:file:/E:/Maven/maven-repository/org
  • 报错:An attempt was made to call a method that does not exist.(试图调用不存在的方法。)
  • 原因:依赖冲突
  • 解决:
  • 1.先重新编译运行试下

2.排查是否是版本不兼容、版本冲突等

以上就是SpringBoot启动失败的原因及其解决方法的详细内容,更多关于SpringBoot启动失败的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文