Do I need thread-safe methods when using Spring

Issue

I am really sorry to ask such a question, but I need a bit of your help.

As far I understand, if I use servlets all my business logic ( methods of which I call in controllers ) should be Thread-safe.

Does the same count for Spring? Do I need to make thread-safe methods in service layer when I use @Getmapping @PostMapping instead of using servlets directly?
I do appreciate your help a lot!

Solution

Thread safety is a different context . Singleton spring beans(default scope) has no relation with thread safety. Spring container only manages life-cycle of objects and guaranteed that only one object in spring container. So, if a Non thread safe object is injected then obviously it is not thread safe. To make it thread safe you have to handle it by coding.

If it is a web-application , Scoperequest can achieve thread-safety as for each new request it creates a new object or Scopeprototype will do this.(for each invocation it creates new bean .)

Answered By – Tarun

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published