BUG排查手册

First Post:

Last Update:

安全无止境,继续前进吧!

又遇到问题了,博士?
工程部已经积累了充足的经验哦!
– by

这个小板块更偏向信息系统开发技术,主要讲述 Java Web 开发过程中的常见 BUG 以及解决方法

Spring Boot 启动失败

端口占用

application.yml配置文件中一般会指定启动端口(没指定则默认 8080 端口)
如果目标端口被占用,就会出现以下报错信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
***************************
APPLICATION FAILED TO START
***************************

Description:

Web server failed to start. Port 8080 was already in use.

Action:

Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.


Process finished with exit code 1

建议更换端口号,必要时关闭占用端口的进程;

依赖注入失败

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean of type 'com.rngad33.ark.service.ThumbService' that could not be found.


Action:

Consider defining a bean of type 'com.rngad33.ark.service.ThumbService' in your configuration.


Process finished with exit code 1

检查要注册的类是否打上了 @Component,@Service 等注解;

循环依赖

Spring IOC 容器注册 Bean 对象存在这样一个机制:如果待注册对象存在依赖的对象,则会优先注册被依赖对象,依此类推;
如果出现了这样一种情况:A 依赖 B,B 依赖 C,C 又依赖 A,就会发生相互等待现象,导致框架启动失败,这种现象就是循环依赖;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

pictureController

myCacheManager
┌─────┐
| pictureServiceImpl
↑ ↓
| jsoupTemplateFromBing
└─────┘


Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.


Process finished with exit code 1

这个问题一般是没有处理好不同对象间的依赖关系导致的,开发者应当优先优化依赖关系,以打破依赖循环;
如果实在没有办法打破,可以使用 @Lazy 注解指定 Bean 对象延迟加载,即使用时才注册到 IOC 容器;

数据库问题

在 Spring Boot 项目中使用了 MyBatis 原生框架或者 MyBatis-Flex 增强框架,你可能会遇到这个启动报错:

1
Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required at org.springframework.util.Assert.notNull(Assert.java:172) ~[spring-core-6.1.15.jar:6.1.15] at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:122) ~[mybatis-spring-3.0.4.jar:3.0.4] at org.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:69) ~[mybatis-spring-3.0.4.jar:3.0.4] at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44) ~[spring-tx-6.1.15.jar:6.1.15] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-6.1.15.jar:6.1.15] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1802) ~[spring-beans-6.1.15.jar:6.1.15] ... 79 common frames omitted

极有可能是缺少 JDBC 相关依赖,需要手动引入(MyBatis-Plus 实测不存在这个问题);

运行时异常

<- To be continued _(:3 ∠)__

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

  _ _   |_  _ _|_. ___ _ |    _ 
| | |\/|_)(_| | |_\  |_)||_|_\ 
     /               |         

   __  __       _           _   _       _____ _           
 |  \/  |_   _| |__   __ _| |_(_)___  |  ___| | _____  __
 | |\/| | | | | '_ \ / _` | __| / __| | |_  | |/ _ \ \/ /
 | |  | | |_| | |_) | (_| | |_| \__ \ |  _| | |  __/>  < 
 |_|  |_|\__, |_.__/ \__,_|\__|_|___/ |_|   |_|\___/_/\_\
         |___/