0

In Java 6, they introduced SPI (service provider interface). It helped to instantiate an object that implements a certain interface, without knowing its exact class, but it only did so for raw types.

However, with SPI, I cannot instantiate parameterized classes like Class<A,B> that implements SomeInterface<A,B>.

How do I do that if I don't want to use any dependency framework like Spring or CDI, in order to keep my library agnostic of those DI frameworks?

Seems like I painted myself into a corner.

Janus Varmarken
  • 2,306
  • 3
  • 20
  • 42
Matthias Bohlen
  • 598
  • 6
  • 12
  • Did you look at [this question](https://stackoverflow.com/questions/5451734/loading-generic-service-implementations-via-java-util-serviceloader)? – vbezhenar Feb 11 '20 at 22:35
  • Remember at run time, generics are erased and only the raw classes exist. So really generics are just syntactic sugar for the compiler. If that's not your use case, generics might not be the right fit for your problem. – markspace Feb 12 '20 at 02:03
  • Thanks, @vbezhenar, that looks like a possible solution! – Matthias Bohlen Feb 12 '20 at 07:43

0 Answers0