简体   繁体   中英

Play framework compile-time dependency injection and singleton

I have been refactoring my Play app from using Guice to using Compile-time DI.

In Guice, when we don't decorate a class with @Singleton , many instances can be created as needed.

In compile-time DI, we create an instance to be injected once, thus I think it is equivalent to a singleton.

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. 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?

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. 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.

It's theorically even better to have singletons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM