Spring store object in session

后端 未结 4 1478
醉梦人生
醉梦人生 2020-12-04 09:15

I would like to implement a shopping cart with Spring, so I need to save an object Cart ( which has attributes like products, paymentType and deliveryType ) in

4条回答
  •  误落风尘
    2020-12-04 09:45

    You just need to add Scope annotation as below with session and proxy mode

    @Component
    @Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
    public class ShoppingCart implements Serializable{
    }
    

    Where ever you need to use shopping cart object, you can autowire it

    @Service
    public class ShoppingCartServiceImpl implements ShoppingCartService {
        Logger logger = LoggerFactory.getLogger(ShoppingCartServiceImpl.class);
    
    
        @Autowired
        ShoppingCart shoppingCart;
    }
    

    Disclosure: I have developed a sample project, which uses spring MVC, angularJS and bootstrap that demonstrate Spring Session scope -
    https://github.com/dpaani/springmvc-shoppingcart-sample

提交回复
热议问题