Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdi-dev] Guice to CDI migration

Hi all,

My name is Amit Mendapara, a full stack software developer with more than 13 years of experience working on different technology stacks like Python, Java and React/Angular are major stacks among them.

One of the project I am working on is using Guice for dependency injection, servlet and JPA integration. We are planning to migrate this project to CDI but I found one case that I don't know how to deal with.

Let me explain once case where I am really not sure how we can handle this. The application structure is like this:

app
├── module-a
└── module-b

The "app" is nothing but a "war" application and "module-a" and "module-b" are normal jar libraries providing persistence entities and services. The modules are not all required, they may be independent or extending other modules. They are build time modules only. So the app can be built with some specific number of modules as required.

In the case above, let's say, "module-a" provides a service "GreetingService" and it's implementation "GreetingServiceImpl" and "module-b" is extending "module-b" by providing an enhanced version of "GreetingSeriveImpl".

The "module-a" binds the service as:

bind(GreetingService.class).to(GreetingServiceImpl.class);

and "module-b" binds the service as:

bind(GreetingServiceImpl.class).to(EnhancedGreetingServiceImpl.class);

The app has a JAX-RS resource utilising this service with dependency injection like this:

@Inject
private GreetingService service;

So now if app is built with only "module-a" it will get "GreetingServiceImpl" for every injected "GreetingService" and if app is built with both "module-a" and "module-b", the app will get "EnhancedGreetingServiceImpl" for every injected "GreetingService".

I have tried using "@Alternative" but I have to use qualifiers to inject this alternative. Is there any way we can make the CDI container use the "EnhancedGreetingServiceImpl" automatically without any qualifiers (without any changes in module-a or app itself).

If required, I can provide a working example of this case but it's not that complex to understand.

Regards
--
Amit Mendapara

Back to the top