Hey there,
there is already a thread about this, but according to the FAQ old Threads should not be revived but rather referenced.
As the original thread is unsolved and lacking decent reproducibility steps, I will try to get things correct here.
Issue recap
Rule java:S6809 (Methods with Spring proxy should not be called via βthisβ) suggest injecting the service into itself to make the calls instead of using this. The Problem is, that Spring discourages the circular references caused by this self injection.
How to reproduce
- Go to start.spring.io and generate a blank project (use maven, as the commands below depend on maven as i donβt know the gradle commands) and language java. all other options should not matter. no additional dependencies needed
- download project and open in IDE of choice
- copy code below into DemoApplication.java below the existing class (or a new file, does not matter)
- run
./mvnw spring-boot:run
to see how spring fails with a error about cirular references (see below); also to ensure the project is built in case sonar needs that - add sonar plugin to the pom.xml in
build > plugins
- run
./mvnw sonar:sonar "-Dsonar.host.url=http://sonarqube"
(adapt required sonar parameter as necessary for own instance)
you can replace./mvnw
with mvn
if you have it on PATH, to avoid downloading another copy of maven.
Sonar Plugin (for reference what I used)
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>5.0.0.4389</version>
</plugin>
Code to copy
// import jakarta.annotation.Resource;
// import org.springframework.scheduling.annotation.Async;
// import org.springframework.stereotype.Service;
@Service
class SonarBug {
@Resource // as suggested by SonarQube; Spring does not like this.
private SonarBug service;
public void badForSonar() {
async();
}
public void goodForSonar() {
service.async();
}
@Async
public void async() {
// empty for demo purposes
}
}
Spring Error
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
βββ->βββ
| sonarBug
βββ<-βββ
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.
Our Sonar Server
Community Build
v25.3.0.104237
Standard Experience
via Docker