Spring: Singleton/session scopes and concurrency

前端 未结 3 752
無奈伤痛
無奈伤痛 2021-02-02 01:48

Does singleton/session scopes of Spring beans require that access to all its fields must be synchronized? Say through \"synchronized\" keyword or using some classes from package

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 02:26

    Basically for every user that initiates a session a new ShoppingCart will be created and scoped to that session per that user.

    Your class is thread-safe. The initialization

    private List items = new ArrayList();
    

    is a thread-safe initialization and

    addItem(Product item) is an atomic operation and is also therefore thread-safe.

提交回复
热议问题