【发布时间】:2018-08-12 18:16:30
【问题描述】:
我有一个 application.properties 文件,其中包含以下内容:
retry.count = 3
注入到 Spring MVC 控制器中
@Value("${retry.count}")
private int retryCount;
try{
invokeRestCall()
}
catch(TimeOutException ex){
if(retryCount > 0) {
retryCount--;
//Retry call with recursion
}
}
由于 retryCount 是在应用程序加载时注入的,而不是会话范围内的,所以 retryCount 在会话之间共享并且计数器不起作用
bean 的范围可以由@Scope(value="session") 定义,但这不适用于@Value 属性并显示错误“无法为字段定义范围”
还有其他方法可以使字段会话范围吗?
【问题讨论】:
-
当您将@Scope(value="session") 放在类级别时,该字段不会成为会话范围吗?
标签: java spring spring-mvc spring-boot