When I started to learn Spring two “difficult” questions was mainly running in my mind:
- How is singleton bean created and then how that single bean autowired in different classes?
- Imagine this case:
There are 2 users which one of them wants to login and another one wants to create report in our application at the same time. And both of login and createReport methods use userService bean which is scoped as singleton. In this case do those methods use that singleton bean sequentially? Otherwise how does singleton bean serve multiple requests concurrently?
To answer them was not difficult as I think. Just it was needed to clarify simple, but important points. That is why I will try to describe them with basic code examples. Let’s begin:
1.It would be useful if we talk about Spring container at first. Because I think this will help you to describe process better in your mind.
Spring container creates beans in it. After creating needed beans it injects dependencies of them. The container gets its instructions by reading configuration metadata(XML or Java annotations). So after Spring container is initialized your application is ready to use as described in following picture:

When you define a bean definition like following you tell container that it must create only one instance for that bean definition in the container:
<bean id=”accountDao” class=”…” scope=”singleton”/>This single instance is stored in a cache of such singleton beans. Then Spring container returns this cached object to all requests and references for beans with that bean definition:

If we want show above example with new() operator for describing simplified view of what Spring container does on startup of application, we can code as following:
I think there is no need for more explanation. Example shows how same bean is used for different classes.
2.In Spring every request is executed in separate thread. For example,when 2 users want to login at the same time, JVM creates 2 threads: one thread for first user, another one for second user.
For showing this, following simple code snippet can help us. If you sent two requests for login method concurrently and print current thread name(which the requested login method is executed), you will see two different thread names there. This shows us every request is executed in separate thread:
Result:
http-nio-8080-exec-9 ----------- Ben ---------- Sat Nov 23 20:01:35
http-nio-8080-exec-6 ----------- Kate ---------- Sat Nov 23 20:01:35 You can send concurrent requests with help of curl command like this:curl http://localhost:8080/login/Ben & curl http://localhost:8080/login/Kate
And these threads work with singleton bean separately. How? Let’s speak about memory allocation in Java a little bit.
In Java every object is created in heap. Heap is globally shared memory. That is why, every thread can access objects in heap.
But stack is used for execution of only one thread. In that thread when a method is invoked a new block is created in stack in LIFO( Last-In-First-Out) order . This block holds local primitive values and reference to other objects in the method. And stack memory cannot be accessed by other threads.
So when we create singleton bean, it resides in heap. Because of heap is accessible from anywhere of application every created thread can point to that singleton bean. And how is this happening? When the thread request the singleton bean, it is going to refer (with help of reference variable in stack) to the bytecode of singleton bean in heap. So multiple threads can refer singleton bean at the same time. The compiler is going to point to the same bytecode and simply execute it and store method specific values in corresponding blocks in stack separately. There is no restriction preventing the compiler from doing this. The only restriction that the Singleton class puts on the JVM is that it can have only one instance of this class in the heap. And this is why ideal singleton bean must be stateless. Otherwise concurrency issues can occur.
There is some points which can be added. But for not being too long I decided to write them in second part. Hope this will help you to understand process clearly.
And if you liked this article, click the 👏 button, please. So let me know how much it was useful for you. Happy coding :)



