Search code examples
javajakarta-eedependency-injectioncdiweld

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


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?


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>