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.
No text available
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");
}
}
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.