How to specify an @Alternative producer method in beans.xml?

cdi, dependency-injection, jakarta-ee, java, weld

Solution

Found it - you can just specify the whole producer class as an alternative.

@Alternative
public class AltProducer { ... }

beans.xml:

<beans>
  <alternatives>
    <class>com.package.AltProducer</class>
  </alternatives>
</beans>

Problem

Say I have: ``` public interface Foo {...} public class AltProducer { private Foo altFoo; @Produces @Alternative public Foo getAltFoo() { return altFoo; } } ``` What do I need to put in beans.xml so that my AltProducer's @Produces method will get called, instead of injecting Bar?

Original source