naveen
naveen

Reputation: 134

Switching among Spring, Guice, Weld or other DI implementations

I am writing a framework on top of it where other teams develop applications.

I want to provide Dependency Injection as part of it. I am making developers uses JSR 330 annotations and my framework can work on DI.

Still i have the following problem

Developer need to write module if i use Guice, beans.xml if I use WELD. This couples specific DI with my developer app code. I want to switch underlying dependency injection in framework without touching any of the application code.

Is there anyway to do it?

Upvotes: 1

Views: 686

Answers (2)

jwells131313
jwells131313

Reputation: 2394

I do not think JSR-330 has enough information about how the system starts in order for it to be truly switchable. For example, if you are using Guice you will need to create an Injector. If you are using Spring you will need to use its JavaConfig. If you are using HK2 you'll need to use either the inhabitant generator or use Modules. If you are using CDI you do only need a beans.xml (and in the later CDI you don't even need that) but you will almost definitely find that you need other features of CDI such as Producers.

I think JSR-330 is too minimal to be portable for these reasons

Upvotes: 0

Jan Galinski
Jan Galinski

Reputation: 11993

If you strictly stick to JSR330 features, it is possible to provide a framework jar that can be used by all three implementing DI-frameworks you mentioned. To allow CDI to scan the module, you should provide a bean.xml, but I wouldn't call that code invasive.

If you need to provide something more than just JSR330, like modules, factories, producer-methods, AutoConfigurations, ... you could offer custom extension modules, so you have "myframework-core" for the implemantation agnostic stuff and "myframework-guice" on top of it providing helpers and modules for guice and so on.

Upvotes: 4

Related Questions