Is this design of Spring singleton beans thread safe?

后端 未结 6 2160
[愿得一人]
[愿得一人] 2021-02-01 04:45

Consider the following Spring Service class. The spring scope defined is Singleton. The two service beans auto-wired as fields in the class below have similar structure - they t

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-01 05:38

    Spring doesn't guarantee thread-safety. That's your responsibility.

    All private member variables are shared. They might be final, but that only means that the references can't be changed. Any mutable state must be synchronized. If they're indeed immutable, then I think you're on solid ground.

    I agree with the comment about autowiring dependencies. I would leave those under Spring's control if possible.

提交回复
热议问题