Spring MVC 应用程序可以是多线程的,即使它的 servlet 不是吗?

发布于 2024-11-06 14:04:42 字数 120 浏览 1 评论 0原文

当您谈论 Spring 应用程序是多线程时,您是否一定是指该应用程序中定义的 servlet 是否是多线程的?

或者,即使应用程序中的 servlet 不是多线程的,Spring 应用程序也可以配置为多线程吗?

When you talk about a Spring app being multithreaded, are you necessarily referring to whether the servlets that are defined in that app are multithreaded?

Or can a Spring app be configured to be multithreaded even if the servlets in the app are not multithreaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉世弥音 2024-11-13 14:04:42

不再支持单线程 servlet。它们已被弃用很长时间,因此所有 servlet 都是多线程的。

然后,spring 不使用 servlet(除了一个 - 调度程序)。它使用bean,可以是控制器、服务和存储库(daos)。

如果这些bean在其字段中不保存任何数据(除了它们的依赖项),那么它们是线程安全的(我想你所说的“多线程”)

简而言之 - 不要在你的spring bean中存储任何数据。将所有必需的数据作为参数传递。

Single-threaded servlets are no longer supported. They have been deprecated for a long time, so all servlets are multithreaded.

Then, spring does not use servlets (apart from one - the dispatcher). It uses beans, which can be controllers, services and repositories (daos).

These beans are thread-safe (what I suppose you mean by "multithreaded") if they don't hold any data in their fields (apart from their dependencies)

In short - don't store any data in your spring beans. Pass all required data as parameters.

梦里梦着梦中梦 2024-11-13 14:04:42

典型的 Java Web 应用程序是多线程的,因为每个请求都在其自己的线程上处理。在此类应用程序中,当您拥有维护状态的对象(例如通过修改静态属性)时,您必须小心,因为它们可能会相互覆盖。

当您谈论 servlet 时,如果两个请求同时到达同一个 servlet,则相关 servlet 代码将同时执行两次。在像 Struts 或 Spring 这样将请求委托给对象的框架中,可以重用同一个 bean 实例,或者可以为每个请求创建一个新的 bean 实例,具体取决于您如何配置框架(即在春天的情况)

typical java web applications are multi-threaded in that every request is handled on its own thread. In such applications, you have to be careful when you have objects that maintain state (via modifying a static property, for example), as they can overwrite each other.

When you are talking about servlets, if two requests come in at the same time to the same servlet, the relevant servlet code is being executed twice concurrently. In frameworks like Struts or Spring, which delegate requests to objects, either the same bean instance can be reused, or a new bean instance could be created for each request, depending on how you have your framework configured (i.e. to use prototypes or singletons in the case of Spring)

彼岸花似海 2024-11-13 14:04:42

Spring MVC 使用单个调度servlet来调用定义的Controller。话虽这么说,控制器应该是无状态的,不包括 Spring 注入的 bean。通过一个操作更改一个控制器的状态可以影响另一操作。

Spring MVC uses a single dispatching servlet that will invoke defined Controllers. That being said, the Controller's should be stateless with the exclusions of Spring injected beans. Changing the state of one controller through one action can effect another action.

花开半夏魅人心 2024-11-13 14:04:42

如果我正确理解你的问题,servlet(或java beans)本身就不需要担心多线程。要创建多个线程,您可以通过在步骤配置中执行以下操作来实例化多线程步骤或并行步骤:

多线程步骤:

<step id="loading"> <tasklet
task-executor="taskExecutor"
throttle-limit="20">...</tasklet>
</step>

并行步骤:

<job id="job1">
<split id="split1" task-executor="taskExecutor" next="step4">
    <flow>
        <step id="step1" parent="s1" next="step2"/>
        <step id="step2" parent="s2"/>
    </flow>
    <flow>
        <step id="step3" parent="s3"/>
    </flow>
</split>
<step id="step4" parent="s4"/>

这些示例可以在此处找到并进行更深入的评论。

If I understand your question correctly, the servlets (or java beans) themselves will not need to worry about multi-threading. To create multiple threads, you would instantiate multi-threaded steps or parallel steps by doing the following from within your step configuration:

Multi-Threaded Step:

<step id="loading"> <tasklet
task-executor="taskExecutor"
throttle-limit="20">...</tasklet>
</step>

Parallel Step:

<job id="job1">
<split id="split1" task-executor="taskExecutor" next="step4">
    <flow>
        <step id="step1" parent="s1" next="step2"/>
        <step id="step2" parent="s2"/>
    </flow>
    <flow>
        <step id="step3" parent="s3"/>
    </flow>
</split>
<step id="step4" parent="s4"/>

These examples can be found and commented on more in-depth here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文