Is this design of Spring singleton beans thread safe?

后端 未结 6 2158
[愿得一人]
[愿得一人] 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:35

    The example code shown in your question is definitely thread-safe.

    However, the code needs to be considered in the context of the whole application. For instance, the code above does not provide any guarantees about the thread safety of the objects referred to by the documentGenerationService and documentPublishService attributes. If they are not adequately synchronized, then code that uses them (including other methods of this class) may not be thread-safe.

提交回复
热议问题