David Jones
David Jones

Reputation: 83

SpringBoot Single Page Application Concurrency

I have copied a sample Spring Boot SPA. I want to understand, what happens if multiple people use the web page via the URL. Does Java create an instance of the web application per call? Memory resources are not shared, right, i.e. if there is a list object appended to, each user sees their own list?

Upvotes: 3

Views: 292

Answers (3)

Nathan Hughes
Nathan Hughes

Reputation: 96434

This isn't shared-nothing like PHP or Rails. Java is so slow starting up that firing up a new instance of the application for every request isn't an option.

Check out the spring-boot sample application source code, there's a main method on the SpringbootJQueryAjaxClientApplication class, like this:

public static void main(String[] args) {
    SpringApplication.run(SpringBootJQueryAjaxClientApplication.class, args);
}

this is the main method like for any Java program, what happens here is it starts the self-hosted servlet container and installs the application into it, then waits for http requests.

It is one process, where every request is served by a thread, so memory is shared. Spring components like com.javasampleapproach.jqueryajax.controller.RestWebController (scoped as singleton by default) are instantiated once in the web application, and every request calls methods on the same instance.

Upvotes: 1

Always Learning
Always Learning

Reputation: 2743

The default scope for a spring-boot bean is a singleton. Assuming your bean is not managing state you should be fine with the default behavior:

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.

Now if you are using a bean that's stateful and want a new bean used per request, you can define the scope of that bean to be prototype:

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.

Upvotes: 1

Compass
Compass

Reputation: 5937

Spring resources like @Service and @Repository and @RestController are stateless singletons. Only a single instance is created to serve the application.

Your implementation of the list at scope level will determine whether or not it is shared. If you define the List in the Controller, as in your example, then every user will have access to the same list. You can use multiple browsers to see that the list is shared. Based on the example, this is fine, as getting "all" should really mean getting all.

If you wanted each user to have their own list, you would have to implement some sort of Session or back-end process to associate each individual user with their own list.

Upvotes: 1

Related Questions