I have projectA, projectB, and projectC Eclipse Maven projects.
ProjectAcontains:IMyApiinterface.- "Empty"
META-INF\beans.xmlfile.
ProjectBcontains:IMyConfiginterface.MyConfigJndiimplementation ofIMyConfig.MyApiImplimplementation ofIMyApi, with a property@Inject private IMyConfig config;.- "Empty"
META-INF\beans.xmlfile.
ProjectCcontains:- a
MyConfigAlterimplementation ofIMyConfig, marked as@Alternative. - a
Mainclass (and method) that initializes Weld SE and retrieves aIMyApibean. - a
META-INF\beans.xmlwhereMyConfigAlteris listed in thealternativessection.
- a
Now, I run the Main class, and the IMyApi bean is successfully retrieved (as a MyApiImpl instance). But such an instance has been, in its config property, injected with a MyConfigJndi instance, instead of the alternative version (MyConfigAlter)
I am using Eclipse Luna + M2Eclipse.
What am I doing wrong?
UPDATE: I found out that using @Specializes instead of @Alternative solves the issue, but I still think it is not the proper solution (in some situation I may not have access to the "default" implementation).
UPDATE 2:
I am using Weld-se, 2.2.10.Final:
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se</artifactId>
<version>2.2.10.Final</version>
<scope>runtime</scope>
</dependency>
And the initialization is simply
WeldContainer weld =
new Weld().
initialize();
IMyApi myApi =
weld.
instance().
select(
IMyApi.
class).
get();
Selecting an alternative using the
alternativeselement in thebeans.xmldescriptor only affects the corresponding bean archive, i.e.ProjectCin your case, as documented in Declaring selected alternatives for a bean archive. Based on that, this is logical that theProjectBbean archive gets theMyConfigJndiimplementation injected.Since CDI 1.2, it is possible to select an alternative globally for the application using the
@Priorityannotation as documented in Declaring selected alternatives for an application.So in your case, you could write: