是Spring MVC控制器单身人士吗? [英] Are Spring MVC Controllers Singletons?

查看:76
本文介绍了是Spring MVC控制器单身人士吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Spring 3 MVC @Controllers 有疑问。当请求进入时,容器是否为每个请求创建一个新的控制器实例(类似于Struts 2中的 Action - ThreadLocal ActionContext )或单个实例响应所有请求?默认情况下是上下文中的控制器bean单例?

I have a query regarding Spring 3 MVC @Controllers. When a request comes in, does the container create a new instance of the controller for each request (Similar to an Action in Struts 2 - ThreadLocal ActionContext) or a single instance responds to all requests? By default are the controller beans singletons in a context?

Spring 3 MVC应用程序的指南/最佳实践是什么?这些设置是否可配置?应该配置?
如果我的环境是集群环境,它会有多大变化 - 我想它不应该因为jvm /容器会有所不同 - 但是一些权威的建议会受到欢迎。

What are the guidelines/ best practices for a Spring 3 MVC application? Are these settings configurable? Should those be configured? Does it change much if my environment is a clustered environment - I guess it should not as the jvm/containers would be different- but some authoritative suggestion would be welcome.

我看过Spring文档,但可能我错过了。任何'这就是我们如何实现'答案/指针/链接会有所帮助
谢谢。

I have read Spring documentation but probably I missed it. Any 'this is how we implemented kind of' answers/ pointers/ links would be helpful Thanks.

推荐答案

春天控制器是单例(每个Web应用程序只有一个控制器实例),就像servlet一样。通常,改变这种行为是没有意义的(如果可能的话)。有关常见的陷阱,请参阅关于servlet的线程安全性,同时适用于控制器。

Spring controllers are singletons (there is just one instance of each controller per web application) just like servlets. Typically there is no point in changing this behaviour (if it's even possible). See Regarding thread safety of servlet for common pitfalls, also applying to controllers.

如果您的应用程序是群集的,请尽可能多地避免状态。控制器中的状态将需要同步以避免线程问题。此外,您可能会跨服务器复制该状态 - 非常昂贵且麻烦。

If your application is clustered do as much as you can to avoid state. State in controllers will require synchronization to avoid threading issues. Also you'll probably replicate that state across servers - very expensive and troublesome.

这篇关于是Spring MVC控制器单身人士吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆