声纳:实例方法不应写入“静态"实例.领域 [英] Sonar: Instance methods should not write to "static" fields

查看:815
本文介绍了声纳:实例方法不应写入“静态"实例.领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am getting this prompt from Sonar: Instance methods should not write to "static" fields

我不确定要解决此问题需要更改什么.

I'm not quite sure what I need to change to fix this issue.

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

Does "SemaMonitorProxy.applicationContext" have to equal a static method?

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;
    }
}

推荐答案

实际上此方法是

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

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

is an instance method writing to a static field:

private static ApplicationContext applicationContext

您不能使上述方法静态化.因此,唯一的解决方案是从applicationContext声明中删除static关键字.

You cannot make the above method static. So the only solution would be to remove the static keyword from the applicationContext declaration.

private ApplicationContext applicationContext

这篇关于声纳:实例方法不应写入“静态"实例.领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆