Home > Mobile >  Play framework compile-time dependency injection and singleton
Play framework compile-time dependency injection and singleton

Time:11-18

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?

CodePudding user response:

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.

  • Related