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.
// 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
Thank you for the report! I filed SONARJAVA-5399 for the issue with the circular dependency and SONARJAVA-5401 for the typo.
I think in this case we will need to allow circular dependency. Did you try to resolve it with @Lazy annotation on the service?
Fyi, I used @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) to resolve the error about circular dependencies with @Cacheable, which shares the same issue.
Are there any updates on this? Ran into the exact same issue as the OP, and the remediation recomendation you gave (marking with @Scope(proxyMode = scopedProxyMode.TARGET_CLASS or @Lazy seem to work, but Iβm a bit worried about applying either of those without understanding the full context of what they change on the execution of the class itself.