我应该为不需要序列化、代理或修饰的 bean 使用 CDI javax.inject.Singleton

Should I Use CDI javax.inject.Singleton for the beans that dont need serialization, proxies, or decorations

我了解 ApplicationScoped 和 CDI javax.inject.Singleton 的工作原理,并且我了解 the difference between these two scopes。我的应用程序有很多不需要序列化、代理或修饰的 bean,因此我正在考虑将它们从 ApplicationScoped(有效)切换到 javax.inject.Singleton 以通过避免代理来提高性能。

我的问题是:我真的应该做这样的改变吗?

当然,您可以对作用域注释进行重构,但您需要注意调用那些单例托管 bean 的 bean 的序列化。参见 http://docs.jboss.org/weld/reference/latest-2.2/en-US/html_single/#_the_singleton_pseudo_scope

我会首先调查我将从这一举措中获得多少性能提升,如果它真的值得花时间我需要投资。通常性能瓶颈来自数据库 query/indexing 性能、I/O、网络、线程锁、低效算法(以及更多),然后是 Java 代理开销,所以我会坚持使用标准的 @ApplicationScoped。

参见: http://ordinaryjava.blogspot.com/2008/08/benchmarking-cost-of-dynamic-proxies.html https://spring.io/blog/2007/07/19/debunking-myths-proxies-impact-performance/