Igor Akkerman
Igor Akkerman

Reputation: 2478

Support multiple DI containers in own Java framework

In my own multi-purpose Java framework, how to use dependency injection without depending on a concrete DI container? That is, any application should be able to use my framework, no matter whether it uses CDI, Spring or Guice itself.

All of the following should be possible:

Upvotes: 1

Views: 400

Answers (2)

Antonin Stefanutti
Antonin Stefanutti

Reputation: 1071

JSR 330: Dependency Injection for Java is the specification with the largest level of consensus within the dependency injection community and as a result is supported by:

That makes JSR 330 the best common denominator to have portability accross the dependency injection frameworks. Which obviously comes at the cost of the range of functionalities.

Upvotes: 3

duffymo
duffymo

Reputation: 308763

You'll have to come up with a lowest common denominator interface for all DI operations you need and implementations for each concrete instance you may want to use.

You can't have any DI engine specific annotations in your objects, so you'll have to externalize all configuration and use setter or constructor injection for all objects.

Personally, I think this is a waste of time. DI ought to be a commodity. I'd see no good reason for switching DI engines. It's more likely that you'll choose one that meets your needs and stick with it.

Upvotes: 1

Related Questions