Java Spring控制器保留旧值
我正在开发一个使用弹簧注入的系统。在某个时间点,屏幕上会显示一条警告,该警告使用WarningHelper类(一个Spring控制器)上的属性显示。以下是代码摘要:Java Spring控制器保留旧值,java,spring,controller,code-injection,Java,Spring,Controller,Code Injection,我正在开发一个使用弹簧注入的系统。在某个时间点,屏幕上会显示一条警告,该警告使用WarningHelper类(一个Spring控制器)上的属性显示。以下是代码摘要: @Controller @Scope(WebApplicationContext.SCOPE_SESSION) public class WarningHelper implements Serializable { //Bunch of attributes private String warningText
@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
}
}
这一切在第一次的时候都很好。问题是,如果我尝试输入另一个具有不同消息的配置文件,第一个配置文件仍会显示。请注意,此更改过程不涉及其他登录,因此会话仍然相同。我试过玩弄这个作用域,但没有用。在警告帮助和自定义URLController类中将
@scope(WebApplicationContext.scope\u会话)
更改为@scope(WebApplicationContext.scope\u请求)
。这将为每个请求实例化CustomUrlController
和warninghelp
。似乎有效。我会再测试一些,但初步测试很好!谢谢