Count.java:如何使用Session範圍的組件,在控制器
@Component
@Scope(value = "session",proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Count {
Integer i;
public Count() {
this.i = 0;
}
控制器:
@Controller
public class GreetingController {
@Autowired private Count count;
@RequestMapping("/greeting")
public String greetingForm(Model model) {
if(count.i == null) i == 0;
else i++;
model.addAttribute("count",String.valueOf(count.i));
return "greeting";
}
}
但我每次運行這個控制器(/賀卡),它總是增加了我,即使我關閉瀏覽器,那麼如何在Singleton控制器中使用此Session Session組件?
[Spring request scope bean]的可能重複(http://stackoverflow.com/questions/14731092/spring-request-scope-bean) – nowszy94
爲'i'創建一個getter。 – zeroflagL
@zeroflagL:它的工作原理,謝謝。但你能解釋一下嗎?起初,會話bean像一個全局變量,它甚至在我刪除cookies時也會增加。但是使用setter/getter來代替i ++;它的作用就像一個魅力 – Snoob