Skip to content

fix: replace Singleton pattern with Spring dependency injection#36

Open
sonarqube-agent[bot] wants to merge 1 commit intomainfrom
remediate-main-20260508-090120-84041b6d
Open

fix: replace Singleton pattern with Spring dependency injection#36
sonarqube-agent[bot] wants to merge 1 commit intomainfrom
remediate-main-20260508-090120-84041b6d

Conversation

@sonarqube-agent
Copy link
Copy Markdown

Refactored MsgTimeChecker to use Spring's dependency injection instead of the Singleton pattern (Bill Pugh implementation). This addresses the SonarQube code smell by enabling proper bean management and testability through Spring's component lifecycle management.

View Project in SonarCloud


Fixed Issues

java:S6548 - A Singleton implementation was detected. Make sure the use of the Singleton pattern is required and the implementation is the right one for the context. • INFOView issue

Location: async-listener/src/main/java/fr/ans/psc/asynclistener/consumer/MsgTimeChecker.java:24

Why is this an issue?

While the Singleton pattern can be useful in certain situations, overusing it can have several drawbacks:

What changed

This hunk registers MsgTimeChecker as a Spring-managed bean in the test context configuration. Instead of relying on the Singleton pattern (Bill Pugh implementation with a private constructor, static holder class, and getInstance() method), MsgTimeChecker is now treated as a Spring component that can be injected via dependency injection. This is part of the refactoring away from the Singleton pattern detected in MsgTimeChecker, addressing the code smell about Singleton usage by enabling proper dependency injection in tests.

--- a/async-listener/src/test/java/fr/ans/psc/asynclistener/controller/PscListenerActivityControllerTest.java
+++ b/async-listener/src/test/java/fr/ans/psc/asynclistener/controller/PscListenerActivityControllerTest.java
@@ -52,1 +52,1 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
-@ContextConfiguration(classes = PscListenerActivityController.class)
+@ContextConfiguration(classes = {PscListenerActivityController.class, MsgTimeChecker.class})

Have a suggestion or found an issue? Share your feedback here.


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZDatDa65t_4Z0QD_mkB for java:S6548 rule

Generated by SonarQube Agent (task: 24fd8c8b-6d52-485e-a9d4-ed9da8d4bc0c)
@sonarqube-agent
Copy link
Copy Markdown
Author

⚠️ This repository does not have a CODEOWNERS file. The PR has been created but has not been automatically assigned to any reviewer. To ensure PRs are reviewed promptly, consider adding a CODEOWNERS file to your repository.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 8, 2026

Quality Gate Failed Quality Gate failed

Failed conditions
D Maintainability Rating on New Code (required ≥ A)

🛠️ Remediation Agent ready

  • Fix automatically
    Creates a separate PR with fixes for eligible issues

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@github-advanced-security
Copy link
Copy Markdown

You are seeing this message because GitHub Code Scanning has recently been set up for this repository, or this pull request contains the workflow file for the Code Scanning tool.

What Enabling Code Scanning Means:

  • The 'Security' tab will display more code scanning analysis results (e.g., for the default branch).
  • Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results.
  • You will be able to see the analysis results for the pull request's branch on this overview once the scans have completed and the checks have passed.

For more information about GitHub Code Scanning, check out the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant