How can we fix the SonarQube issue in AEM where constructor injection should be used instead of field injection? We are encountering the mentioned issue when using the @Injection annotation. | Community
Skip to main content
Level 2
July 5, 2023
Solved

How can we fix the SonarQube issue in AEM where constructor injection should be used instead of field injection? We are encountering the mentioned issue when using the @Injection annotation.

  • July 5, 2023
  • 4 replies
  • 1821 views
No text available
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Nishant-Singh

It seems you are using Spring J2EE rules also in SonarQube. 

if Spring is not used then you can remove those rules.

if its really required to fix then you can write as below -

 

 

 

import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.ScriptVariable; import org.apache.sling.models.annotations.injectorspecific.SlingObject; import com.day.cq.wcm.api.Page; import javax.inject.Inject; import javax.inject.Named; @Model(adaptables = {SlingHttpServletRequest.class, Resource.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class TestModelConstructor { private String currentResourcePath; private String currentPagePagePath; private String requestParam; public TestModelConstructor( @ScriptVariable @Named("currentPage") final Page currentPage, @ScriptVariable @Named("resource") final Resource resource, @SlingObject @Named("slingHttpServletRequest") final SlingHttpServletRequest slingHttpServletRequest ) { currentResourcePath = resource.getPath(); currentPagePagePath = currentPage.getPath(); requestParam = slingHttpServletRequest.getParameter("myParam"); } }

 

 

 

 

4 replies

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
July 5, 2023

A couple of things to check:
1) Are you using "javax.inject.Inject" annotation?

2) Are you using the specific annotation whenever possible instead of "Inject" all over the place?

 

I am not sure if you really want to use constructor injection instead, it maybe depends on the scenario, Can you post a sample code?

 
Esteban Bustamante
aanchal-sikka
Community Advisor
Community Advisor
July 6, 2023

hello @nesan 

 

Requesting you to please share the code (atleast the snippets) and the exact error that Sonarqube is reporting.

Aanchal Sikka
Tanika02
Level 7
July 6, 2023

Hello @nesan - 

 

  1. Do you have the AEM RulesSet Enabled/Configured in SonarQube? As I have not seen this error in recent times while using SonarQube!
  2. Additionally, there is definitely a way how you can handle this via instead of using field injection, refactor the classes to use constructor injection. But it might not work pretty well if we do such refactoring at the Sling Model level or that would require higher efforts to handle.

Now we can definitely add exception to sonarQube to handle this but I would recommend raising an Adobe support case.

Nishant-Singh
Adobe Employee
Nishant-SinghAdobe EmployeeAccepted solution
Adobe Employee
July 6, 2023

It seems you are using Spring J2EE rules also in SonarQube. 

if Spring is not used then you can remove those rules.

if its really required to fix then you can write as below -

 

 

 

import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.ScriptVariable; import org.apache.sling.models.annotations.injectorspecific.SlingObject; import com.day.cq.wcm.api.Page; import javax.inject.Inject; import javax.inject.Named; @Model(adaptables = {SlingHttpServletRequest.class, Resource.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class TestModelConstructor { private String currentResourcePath; private String currentPagePagePath; private String requestParam; public TestModelConstructor( @ScriptVariable @Named("currentPage") final Page currentPage, @ScriptVariable @Named("resource") final Resource resource, @SlingObject @Named("slingHttpServletRequest") final SlingHttpServletRequest slingHttpServletRequest ) { currentResourcePath = resource.getPath(); currentPagePagePath = currentPage.getPath(); requestParam = slingHttpServletRequest.getParameter("myParam"); } }