Okay, so there is an explicit step that must be taken for a producer method to win over a @Current bean (does not have additional bindings) of that type. I suspect in most cases developers will choose to use the binding type since that is the type-safe approach.
The designation of the alternative class in beans.xml makes all producer methods in that class have a higher precedence than any @Current bean of that type, correct? Is it also possible to specify a single method in beans.xml? There is alluded to in the Policy section, but no examples to support it.
Btw, I think this would be a lot clearer if it was explicitly stated in the spec how a producer method relates to a @Current bean of that type. I guess you could say it can be implied, but the first time someone goes to create a producer method, I can see them not realizing that they have to take an extra step to make it the preferred source.
-Dan
No.
On 22 Jul 2009, at 03:18, Dan Allen wrote:
After the recent revisions of the JSR-299 spec, I'm finding that I'm a tad confused about the bean selection algorithm when a producer method exists.
Consider the following:
class PotatoChip {
}
class SnackFactory() {
public @Produces PotatoChip createPotatoChip() {
return new PotatoChip();
}
}
1) Does the produced PotatoChip take precedence over the raw dependent type when being selected for injection?
either:
2) How do I get the Bean<PotatoChip> that represents the producer method?
BeanManager#getBeans(PotatoChip.class) returns two beans that are indistinguishable from the public API.
@Alternative<beans>
class SnackFactory() {
public @Produces PotatoChip createPotatoChip() {
return new PotatoChip();
}
}
<alternatives>
<alternative>SnackFactory</alternative>
</alternatives>
</beans>
or
class SnackFactory() {
public @Produces @Snack PotatoChip createPotatoChip() {
return new PotatoChip();
}
}
BeanManager.getBeans(PotatoChip.class, new AnnotationLiteral<Snack>() {});
where @Snack is a binding typeAs above.
3) What if the producer method is @RequestScoped? How do I get the request-scoped produce instead of the dependent bean?
Bean<PotatoChip> potatoChipBean = null;
for (Bean<PotatoChip> candidate : getBeans(PotatoChip.class))
{
if (candidate.getScopeType().equals(RequestScoped.class))
{
potatoChipBean = candidate;
break;
}
}
Sorry if this sounds like a dumb question. The spec has changed a lot and my memory is getting mixed in with the current set of facts.
It's possible the RI doesn't actually reflect this behavior atm - if you have a problem, make sure there is a test, then change the RI or file a WBRI.