Ankit Bansal
Ankit Bansal

Reputation: 2348

Spring boot Class Behaviour

I am using Spring boot. I have some question regarding the singleton behaviour of spring boot beans. I am using beans which are singleton. So they will have only one instance per application.

But doubt I have is that

a) Application will create just 1 instance for each request OR application will create just 1 instance for all Requests ?

b) I have create one Service & one static variable in it. SO what is happening now is that in one request I am changing the value & in another request, if am getting the variable, I am getting the changed values? Why is that?

c) ALso same behaviour exists when the variable are not static, just normal class variable? Why is that?

d) Also what would be the good way to store the data which is request specific e.g. Lets say in each request I generate transaction Id & I want to use it in 10 different places in request. So how do I save it per request or do I have to pass it every where. Consider I have 1000 of requests per second. So obviously I can't set class level variables otherwise values will be overridden every time as beans are not thread-safe.

Upvotes: 1

Views: 1916

Answers (2)

pvpkiran
pvpkiran

Reputation: 27058

Some thoughts.
Regarding the singleton behaviour of spring boot beans --> Singleton behaviour is from Spring, not exactly related to springboot as such.

a) Application will create just 1 instance for each request OR application will create just 1 instance for all Requests ? ---> Application will create only once instance of that class in the entirety of the application unless specified otherwise with @Scope.

b) I have create one Service & one static variable in it. SO what is happening now is that in one request I am changing the value & in another request, if am getting the variable, I am getting the changed values? Why is that? ---> As you understand from my previous answer there will be only one instance of the class, and static variable by its nature is shared across multiple invocations. Hence you see the same value.

c) ALso same behaviour exists when the variable are not static, just normal class variable? Why is that? ---> Since its the same instance that is being returned to you everytime when you request for a bean(through @Autowired or other ways), you get the same value for the variable

d) Also what would be the good way to store the data which is request specific e.g. Lets say in each request I generate transaction Id & I want to use it in 10 different places in request. So how do I save it per request or do I have to pass it every where. Consider I have 1000 of requests per second. So obviously I can't set class level variables otherwise values will be overridden every time as beans are not thread-safe. ---> One of the biggest design aspect in spring project is to identify which parts will be used per request and which can be shared. It will be difficult to comment on without knowing more details. But just from your description, I would suggest not to make this class a Bean and create an instance of it everytime. Anyways the lifespan of these objects are short(per request) and will be garbage collected. Or think about using request scopr for this bean @Scope(value="request")

Hope it helps

Upvotes: 3

Poorvi Nigotiya
Poorvi Nigotiya

Reputation: 458

Singleton classes has single instances that are used throughout the application.

a) Application will create just one instance which is used throughout application in all requests.

b) Static variable are class level variables so if value changed in one request, then automatically will be reflected in all other requests.

Same for any other class level variables value will remain same throughout.

c) Same as b)

d) If you can to user some data throughout request then you do it by defining the scope of class as session. This way you can keep data request specific. http://docs.oracle.com/javaee/6/tutorial/doc/gjbbk.html

Upvotes: 1

Related Questions