SoFunction
Updated on 2025-03-03

SpringBoot solves loop calling problem

SpringBoot solves loop calls

This article only records the problems encountered by oneself and solves them.

background

The project is a version of springboot-1.5, migrated to Alibaba Cloud, and upgraded the version of springboot to 2.6. At the beginning, the jetty container was used to start locally, but in the end, the project needs to be migrated to ali cloud and K8S is used as the container, so the project packaging method was finally changed to a jar package.

There was no problem when Jetty was running, but the problem was exposed when using the built-in tomcat.

The reason should be that circular references are not encouraged for springboot 2.6, so they will be exposed directly. The most direct, simple and crude method:

The first solution

The following are configuration operations that allow loop calls:

-circular-references=true

The second solution

Because in the simplest way to make loop calls, the first appearance was made on purpose by oneself, and the second appearance was encountered at that time. Therefore, both appearances are recorded.

The so-called loop call is nothing more than a conflict between springboot when loading the object. The solution is explained in detail below.

Appearance 1:

This is the simplest and easiest to discover in a loop call:

2022-10-02 08:44:55.456  WARN 17112 --- [           main] : Exception encountered during context initialization - cancelling refresh attempt: : Error creating bean with name 'a': Unsatisfied dependency expressed through field 'b'; nested exception is : Error creating bean with name 'b': Unsatisfied dependency expressed through field 'a'; nested exception is : Error creating bean with name 'a': Requested bean is currently in creation: Is there an unresolvable circular reference?
2022-10-02 08:44:55.456  INFO 17112 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-10-02 08:44:55.472 ERROR 17112 --- [           main]   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

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

┌─────┐
|  a (field private )
↑     ↓
|  b (field private )
└─────┘

Appearance 2:

This directly causes tomcat to fail to start, and it fails directly when closing tomcat, and there is a memory leak problem. The error is the same. I pasted a copy from the Internet.

 07-Sep-2020 19:09:11.196 WARNING [localhost-startStop-1] The web application [system] appears to have started a thread named [pool-1-thread-4] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 (Native Method)
 (:175)
 $(:2039)
 (:442)
 (:1074)
 (:1134)
 $(:624)
 (:748)

  • The appearance was seen directly on the console, nothing to say
  • Appearance 2 is directly prompted when starting an application, which will cause memory leakage, but there is no specific error message.

When the solution was solved, it was divided into the following steps:

Method to debug mode. Adjust the logbcak log level to debug, but the effect is not very obvious. Still can't see specific error messages.

2. Then I can only guess blindly and directly go to the breakpoint to give various config annotations, datasource. Fortunately, I reported an error when loading datasource.

  • Then let go of the breakpoint, and there was no problem with Table 2. The reason was that the filter configuration in the druid connection pool we used was finally commented out and loaded successfully, but the problem still exists.
  • Then just start from the breakpoint and load debug all the way. Finally, I found a problem when the create bean was discovered, and the problem with loop calls was discovered.
  • OK, after processing it, the local area can be run normally. However, when uploading to the server, the error of the 2 still reports.

3. Since you already know that it is a loop call problem, then search for the logback configuration online. Let it print the loading and calling logs of each object directly

Compatible with it!!

<logger name="">
	<level value="trace"/>
	<appender-ref ref="stdout"/>
</logger>

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.