gpt4 book ai didi

java - Sonar : Instance methods should not write to "static" fields

转载 作者:行者123 更新时间:2023-12-02 02:28:06 35 4
gpt4 key购买 nike

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

最佳答案

其实这个方法:

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

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

private static ApplicationContext applicationContext

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

private ApplicationContext applicationContext

关于java - Sonar : Instance methods should not write to "static" fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47529300/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com