1 Reply Latest reply on Feb 11, 2016 5:38 AM by Tomas Remes

    CDI alternatives with multiple archives

    John Sanda Apprentice

      I have a CDI alternative producer method in one JAR. I have tested and verified that I am able to use an alternative producer method in an Arquillian integration test. I am running on WildFly 10. Here is the relevant code,


      public class DefaultObjectMapperProducer {
         public ObjectMapper getMapper() { ... }
      // Stereotype annotation
      @Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
      public @interface CustomObjectMapper {
      // alternative producer
      public class TestObjectMapperProducer {
         public ObjectMapper getMapper() { ... }


      and here is my beans.xml (used for testing only),


      <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
             version="1.1" bean-discovery-mode="annotated">


      I have verified that things work as expected in my integration test, but in the test both producer classes are deployed in the archive/JAR. I have another test that deploys a WAR with the default producer in WEB-INF/lib and the alternative in WEB-INF/classes. The beans.xml that is packaged in the WAR looks similar to the example above. And then there is an empty beans.xml that is packaged in the JAR file in which the default mapper resides.


      I enabled debug logging for Weld and I see a couple relevant messages,


      DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-7) WELD-000106: Bean: Producer Method [ObjectMapper] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces @CustomObjectMapper public CustomObjectMapperProducer.getMapper()]
      DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-7) WELD-000103: Enabled alternatives for Weld BeanManager for /content/80dcabb1-33de-4feb-a967-a5100a4683fd.war [bean count=2]:
        - interface CustomObjectMapper


      Based on those log messages it looks like Weld is discovering my alternative producer, but it is not getting used in place of the default. I also tried added the @Priority annotation to my alternative class, but that did not help. How can I make a CDI alternative work when it lives in a separate archive from the original bean?