声纳:实例方法不应写入“静态”字段

时间:2017-11-28 10:31:07

标签: java spring static sonarqube

我从Sonar收到此提示:实例方法不应写入“静态”字段

我不太清楚我需要改变什么才能解决这个问题。

“SemaMonitorProxy.applicationContext”是否必须等于静态方法?

public class SemaMonitorProxy implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    public void registerFailedLoginAttempt(HttpServletRequest request, HttpServletResponse response) {
        final SemaMonitor semaMonitor = applicationContext.getBean(SemaMonitor.class);
        semaMonitor.registerFailedLoginAttempt(request, response);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SemaMonitorProxy.applicationContext = applicationContext;
    }
}

1 个答案:

答案 0 :(得分:1)

实际上这个方法:

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    SemaMonitorProxy.applicationContext = applicationContext;
}

是写入静态字段的实例方法:

private static ApplicationContext applicationContext

您不能将上述方法设为静态。所以唯一的解决方案是从applicationContext声明中删除static关键字。

private ApplicationContext applicationContext