javacdiwelddeltaspike

Java CDI select alternative without priority


I'm trying to replace a default implementation provided by DeltaSpike (LocaleResolver) with my own implementation via @Alternative.

@Alternative
@RequestScoped
public class ExampleLocaleResolver implements LocaleResolver {

}
<alternatives>
  <class>org.example.app.ExampleLocaleResolver</class>
</alternatives>

It seems the only way to make ExampleLocaleResolver the selected bean is by using @Priority or @Specializes.

For example, the following two works perfectly fine:

@Priority(1)
@Alternative
@RequestScoped
public class MyLocaleResolver implements LocaleResolver {

}
@Specializes
@RequestScoped
public class MyLocaleResolver extends DefaultLocaleResolver {

}

My understanding is that with CDI 1.1+, it should be possible to override beans from libraries with alternatives without any kind of hacks.

Could someone please help me understand why I'm unable to inject the @Alternative bean without the @Priorty annotation?


Solution

  • (Warning: I'm weak in the @Alternative area, but I think I understand this and can answer it accurately.)

    I think the problem is that without using @Priority you are restricted to making assertions about bean archives, and your bean archive is not the same as the bean archive from which the DeltaSpike LocaleResolver is sourced. My understanding is in CDI 1.1+ the only way to cause application-wide @Alternative selection to happen is with the @Priority annotation.