[英]Play framework compile-time dependency injection and singleton
I have been refactoring my Play app from using Guice to using Compile-time DI.我一直在重构我的 Play 应用程序,从使用 Guice 到使用编译时 DI。
In Guice, when we don't decorate a class with @Singleton
, many instances can be created as needed.在 Guice 中,当我们不使用@Singleton
装饰一个类时,可以根据需要创建许多实例。
In compile-time DI, we create an instance to be injected once, thus I think it is equivalent to a singleton.在编译时DI中,我们创建了一个注入一次的实例,因此我认为它相当于一个单例。
My question is if I would lose any performance by restricting everything to be only one instance.我的问题是,如果将所有内容都限制为一个实例,我是否会失去任何性能。 For example, if I have an instance serviceA
, with method doSomething
, and considering that everything is stateless.例如,如果我有一个实例serviceA
,使用方法doSomething
,并考虑到一切都是无状态的。 If I have a 32-core CPU, and lots of requests come in. Would Play, in the context of compile-time DI, be able to utilize the full capacity of the CPU?如果我有一个 32 核 CPU,并且有很多请求进来。在编译时 DI 的上下文中,Play 是否能够利用 CPU 的全部容量?
AFAiK Guice (and other runtime DI frameworks) doesn't by default produce singletons for the sole reason to be faster when creating the instances and simplify complex (potentially cyclic) dependency graph. AFAiK Guice(和其他运行时 DI 框架)在默认情况下不会产生单例,唯一的原因是在创建实例和简化复杂(潜在循环)依赖图时更快。 Their goal is to start faster.他们的目标是更快地开始。
Whether you have 1 or 2 instances of ServiceA
will not affect the performance of using these instances once they are created.无论您有 1 个还是 2 个ServiceA
实例,一旦创建这些实例,都不会影响使用它们的性能。
It's theorically even better to have singletons.从理论上讲,拥有单身人士会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.