Spring bean singleton bean:多线程访问 [英] Spring bean singleton bean: multi thread access

查看:59
本文介绍了Spring bean singleton bean:多线程访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设计模式中的单例 bean 应该是单实例的.
因此,如果一个线程的执行正在执行阻塞一系列 bean(比如 @Controller 然后是 @Service 然后是简单 bean)一个接一个

Singleton beans by design pattern should be of single instance.
Hence, if a thread's execution is on the way of execution blocking a series of beans (say @Controller then @Service then simple beans) one after another

  • 如果许多其他具有不同功能但使用相同系列 bean 的并发线程运行会发生什么 - 我的意思是可能会出现数据/值的任何冲突情况,或者 Spring Singleton 是线程安全的?

如果不一样怎么解决?

推荐答案

控制器,作为单例,不是线程安全的.

Controllers, as singleton, aren't thread safety.

我认为您的单例 bean 应该无状态.

I think your singleton beans should be stateless.

但是您可以使用 proxyMode 注入其他请求或会话范围的 bean,例如 http://healthycoder.in/session-scoped-bean-in-a-controller-spring-mvc/

But you can inject other request or session scoped beans using proxyMode, for instance http://healthycoder.in/session-scoped-bean-in-a-controller-spring-mvc/

这篇关于Spring bean singleton bean:多线程访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆