Skip to main content

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

Hi Amit,

the @Alternative should be the solution to this.
However, note that any alternative which you declare with just this annotations is by default *disabled*.
You can enable it locally (for this bean archive) via beans.xml or globally via @Priority annotation added onto the class - go with @Priority, beans.xml won't help since your WAR is a different bean archive from the JAR.

If this would work then I would recommend it over @Specializes. In fact, @Specializes has a very narrow range of use cases where you truly need it.
The difference between the two is that with alternatives, the original bean is still recognized but never instantiated, e.g. any producer on the original bean would still work.
Whereas @Specializes completely disables the original bean.
Also, if you have a set of modules that you build with and that possibly override each other in impls, then @Alternative allows you to easily specify the winner simply by using appropriate number inside the @Priority annotation.
You can have several priorities and there will always be just one "winner" as a candidate for injection.

Matej

----- Original Message -----
> From: "Amit MENDAPARA - Axelor" <amit@xxxxxxxxxx>
> To: "cdi developer discussions" <cdi-dev@xxxxxxxxxxx>
> Sent: Wednesday, March 11, 2020 6:37:32 AM
> Subject: Re: [cdi-dev] Guice to CDI migration
> 
> Hi Emily,
> 
> 
> 
> 
> 
> 
> 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).
> I don't think you need to use any qualifier here. You need to use `@Priority`
> to enable the alternative. The other way to completely override a bean, you
> can use `@Specializes`. By the way, I am not 100% sure what your use case
> is. A working example helps if you need further assistance.
> 
> 
> I never used CDI before so it's new to me and I don't know many of it's
> concepts (I know guice very well so at least DI and AOP concepts might be
> similar). So I will try your suggestion but on twitter one user recommended
> me to check `@Specializes` and that seems the proper solution as per it's
> javadoc.
> 
> The project I am going to migrate is relatively complex and there are few
> more cases I would like to discuss here. I will try to provide a test case
> example that covers some of them.
> 
> Regards
> --
> Amit Mendapara
> 
> _______________________________________________
> cdi-dev mailing list
> cdi-dev@xxxxxxxxxxx
> To unsubscribe from this list, visit
> https://www.eclipse.org/mailman/listinfo/cdi-dev
> 



Back to the top