JavaSpring控制器保留旧值
我正在开发一个使用弹簧注入的系统。在某个时间点,屏幕上会显示一条警告,该警告使用WarningHelper类(一个Spring控制器)上的属性显示。以下是代码摘要:
@Controller
@Scope(WebApplicationContext.SCOPE_SESSION)
public class WarningHelper implements Serializable {
//Bunch of attributes
private String warningText;
//Bunch of other methods
private String configureWarning(Integer caller, Outcome outcome, WarningReturn warningReturn, GlobalWebUser user) {
//business logic
if (hasWarning()) {
setWarningText(warningReturn.getWarningText());
}
return redirect;
}
}
那部分工作得很好。稍后,一个xhtml页面将使用另一个控制器显示此警告,其中第一个控制器被注入。以下是第二个控制器的编辑代码:
@Controller
@Scope(WebApplicationContext.SCOPE_APPLICATION)
public class CustomUrlController {
//Bunch of other attributes
@Inject
private WarningHelper warningHelper;
//Bunch of methods
public String getAllMessages() {
String completeMessage = "";
//business logic creating the message
completeMessage += warningHelper.getWarningText();
return complete Message
}
}
这一切在第一次的时候都很好。问题是,如果我尝试输入另一个具有不同消息的配置文件,第一个配置文件仍会显示。请注意,此更改过程不涉及其他登录,因此会话仍然相同。我试过摆弄一下这个范围,但没用
# 1 楼答案
在WarningHelper和CustomUrlController类中将
@Scope(WebApplicationContext.SCOPE_SESSION)
更改为@Scope(WebApplicationContext.SCOPE_REQUEST)
。这将为每个请求实例化CustomUrlController
和warningHelper