简体   繁体   English

Play 框架编译时依赖注入和单例

[英]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.

相关问题 编译时依赖项注入项目中的EvolutionsComponents - EvolutionsComponents in compile-time dependency injection play project Play 2.4 WebSocket在使用编译时依赖项注入时抛出InstantiationException - Play 2.4 WebSocket throws InstantiationException when using compile-time dependency injection Play Framework依赖注入Object vs @Singleton Class - Play Framework dependency injection Object vs @Singleton Class 使用Playframework 2.6 Macwire进行设置,使用编译时依赖性注入进行测试 - Setting up, Testing with compile-time Dependency Injection, with Playframework 2.6 Macwire 使用play.api.cache.Cache和编译时依赖注入 - Using play.api.cache.Cache with compile time dependency injection 将现有的Play静态路由与新的“编译时”依赖注入的控制器实例混合 - Mixing existing Play static routes with new 'compile-time' dependency-injected controller instances 使用依赖注入来播放框架PathBindable - Play Framework PathBindable with Dependency Injection Play框架中的Scala依赖注入 - Scala Dependency Injection in Play Framework Play Framework 没有依赖注入? - Play Framework without dependency injection? Play框架的SQL运算符的编译时魔术如何工作? - How is the compile-time magic of the Play framework's SQL operator working?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM