ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-SpringBoot单页应用程序并发

2019-11-09 02:02:57  阅读:12  来源: 互联网

标签:spring-boot concurrency single-page-application java



我已经复制了一个示例Spring Boot SPA.我想了解,如果多个人通过URL使用网页会发生什么. Java是否为每个调用创建Web应用程序的实例?内存资源不共享,对,也就是说,如果附加了一个列表对象,每个用户都会看到自己的列表吗?

解决方法:

spring-boot bean的默认范围是单例.假设您的bean不在管理状态,那么您可以使用默认行为:

https://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s04.html

4.4.1 The singleton scope

When a bean is a singleton, only one shared instance of the bean will be managed, and all requests for beans with
an id or ids matching that bean definition will result in that one
specific bean instance being returned by the Spring container.

To put it another way, when you define a bean definition and it is
scoped as a singleton, then the Spring IoC container will create
exactly one instance of the object defined by that bean definition.
This single instance will be stored in a cache of such singleton
beans, and all subsequent requests and references for that named bean
will result in the cached object being returned.

现在,如果您使用的是有状态的bean,并且希望每个请求使用一个新bean,则可以将该bean的范围定义为原型:

4.4.2 The prototype scope

The non-singleton, prototype scope of bean deployment results in the creation of a new bean instance every time a
request for that specific bean is made (that is, it is injected into
another bean or it is requested via a programmatic getBean() method
call on the container). As a rule of thumb, you should use the
prototype scope for all beans that are stateful, while the singleton
scope should be used for stateless beans.



标签:spring-boot,concurrency,single-page-application,java

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有