前言:
对于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启动失败的资料请关注代码网其它相关文章!
发表评论