Sam
Sam

Reputation: 2065

Spring injected bean thread safety

I am having a kind of 'Task' class that contains a spring injected bean. ( will set via setter method)

this Task class will be initiated as a new object and will pass in to the theradpool.

so every task class has that injected bean in it.

when i use a method in that bean class will it be thread safe? ( since the bean is a singleton)

also please note that there are no class level variables defined in that bean class.

Appreciate your help..

Upvotes: 3

Views: 2273

Answers (2)

Marko Topolnik
Marko Topolnik

Reputation: 200148

whether your solution is really thread-safe depends on great many details that you haven't provided, but one item that is obvious from your problem statement is the question of thread-safety of the injected bean. The singleton itself might not be thread-safe.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691635

A stateless object is always inherently thread-safe. Since it uses only local variables, there is no way for one thread to corrupt the state used by another concurrent thread.

Upvotes: 3

Related Questions