Dependency Injection

Red IO redio.development at gmail.com
Mon Feb 20 17:59:43 UTC 2023


Reason/Background:
I was building a server application (in java) designed around the mediator
pattern and dependency injection inspired by a c# server application
tutorial. So I was researching but every dependency injection framework was
relying on bytecode manipulation to do the injection.
For background I really dislike bytecode manipulation for production code.
So I researched what the jdk provides and what not. I discovered that the
jdk provides service loading capabilities in form of the ServiceLoader api.
But it is pretty barebones only allowing creation of service
implementations providing a no-args constructor.

Idea:
I thought that this could be expanded to allow loading of services that
depend on other services (In form of constructor parameters) that can be
loaded with associated ServiceLoaders.
So I coded a small test wrapper for ServiceLoader that does the injection
of known services.
I used MethodHandles to create factory handles that use the associated
loaders to fill in the constructor and in that way avoided bytecode
manipulation.

Suggestion:
I was wondering if this concept would be interesting to incorporate into
the jdk as an expansion of the ServiceLoader api.

Great regards
RedIODev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230220/46fda387/attachment.htm>


More information about the amber-dev mailing list