Is this design of Spring singleton beans thread safe?

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

    You can put the @Autowired annotation on top of the services instead of using them in the constructor. It's a Spring managed bean which means it's a singleton. It is thread safe, but that depends on the implementation.

    @Service     
    public class DocumentService {  
    
      @Autowired
      private DocumentGenerationService documentGenerationService;
    
      @Autowired
      private DocumentPublishService documentPublishService;
    
    ... methods follow
    

提交回复
热议问题