Dependency Injection
John Hendrikx
hjohn at xs4all.nl
Tue Feb 21 17:21:39 UTC 2023
Hi,
I wrote my own DI framework, and byte code manipulation is not a
requirement to do DI. The basic functionality is provided with
reflection only. Most frameworks should be doing exactly that.
Byte code manipulation is only a requirement when the same can't be
achieved by using a JDK interface based proxy. If there is no suitable
interface that can be proxied in such a way, then the only option left
is to create a dynamic subclass. This is often done for injecting
scoped dependencies in a dependency with a different scope (ie, a
request scoped dependency injected into a singleton requires the request
scoped dependency to be proxied). Other features that may require byte
code manipulation are assisted injection, wrappers, AOP, etc.
--John
------ Original Message ------
>From "Red IO" <redio.development at gmail.com>
To "amber-dev" <amber-dev at openjdk.org>
Date 20/02/2023 18:59:43
Subject Dependency Injection
>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/20230221/e78002a0/attachment.htm>
More information about the amber-dev
mailing list