SONAR complains about enabling the static method or removing this set

My program has the following code snippet, and I run SonarQube 5 to check the quality of the code after integrating it with Maven.

However, Sonar asks to make the inclusion method "static" or remove this set. method setApplicationContext.

How to remove this error? Why is this error occurring?

public class SharedContext implements ApplicationContextAware {
public static final String REPORT_ENGINE_FACTORY = "reportEngineFactory";
private static ApplicationContext applicationContext;

public void setApplicationContext(ApplicationContext applicationContext)
        throws BeansException {
    SharedContext.applicationContext = applicationContext;
}

public static ApplicationContext getApplicationContext() {
    return applicationContext;
}

public Object getBean(String name) {
    return applicationContext.getBean(name);
} }
+4
source share
1 answer

To be specific, you seem to be asking about rule S2696: "Instance methods should not write to" static "fields"

As a description of the rule description:

static , , / . static synchronized static.

, , , ( setApplicationContext) static, , static (.. ) applicationContext. , synchronized, .

+5

Source: https://habr.com/ru/post/1654109/


All Articles