Dependency Injection

forax at univ-mlv.fr forax at univ-mlv.fr
Mon Feb 20 21:41:53 UTC 2023


----- Original Message -----
> From: "Kasper Nielsen" <kasperni at gmail.com>
> To: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "Red IO" <redio.development at gmail.com>, "amber-dev" <amber-dev at openjdk.org>
> Sent: Monday, February 20, 2023 9:56:14 PM
> Subject: Re: Dependency Injection

> On Mon, 20 Feb 2023 at 20:25, Remi Forax <forax at univ-mlv.fr> wrote:
>>
>> ----- Original Message -----
>> > From: "Kasper Nielsen" <kasperni at gmail.com>
>> > To: "Red IO" <redio.development at gmail.com>
>> > Cc: "amber-dev" <amber-dev at openjdk.org>
>> > Sent: Monday, February 20, 2023 8:57:20 PM
>> > Subject: Re: Dependency Injection
>>
>> >> Suggestion:
>> >> I was wondering if this concept would be interesting to incorporate into the jdk
>> >> as an expansion of the ServiceLoader api.
>> >>
>> >
>> > I think the practical use cases for this are going to be very limited.
>> > ServiceLoader always creates a new service per request. So if both B and C
>> > depend on A. Two instances of A will be created. Which is rarely what
>> > you want when you are composing an application.
>>
>> It depends if you implement the static method provider or use the default
>> constructor.
> 
> Of course, you could cache the instance in the static method provider.
> But that is even more code. On top of all the managing of individual services
> in META-INF/services or module-info.java you also need to do.

Sadly it's not even "or", it's "and" because you can always put a modular jar into the classpath (not with Maven but it's too easy with Gradle) and in that case only the META-INF/services will be read, the module-info.class will be ignored.

> 
> 
> /Kasper

Rémi


More information about the amber-dev mailing list