When ever a spring boot application started, On startup an object is created and we just map the object using @Autowired.
How does only one object serve multiple request? (default configuration is singleton).
CodePudding user response:
If you use the default bean scope which is singleton you need to make them thread safe yourself, in most cases this mean you should keep keep them stateless.
If you need beans that are scoped to a specifik web request you can use the bean scope request
for this.
You can read more about this in the documentation.
CodePudding user response:
How does only one object serve multiple request
Normally you have that single object representing a service, a controller, a repository etc...
So you just have a reference for a single instance, and that is enough because in singletons you don't have any state on class level, per request. The http request would normally invoke just some method of that singleton and that is fine, since the method works in it's own scope, per thread execution.
So each request normally uses a different thread to execute and each thread when executing a method of the singleton, uses it's own method parameters in it's own stack frame memory.