5 Replies Latest reply: Apr 3, 2012 3:49 AM by Steve Hostettler RSS

Changes in Weld 1.1.6

Steve Hostettler Newbie

Today I migrated to Weld 1.1.6 and I've got the following error message:

 

org.jboss.weld.exceptions.DeploymentException: WELD-001409 Ambiguous dependencies for type [EntityManagerFactory] with qualifiers [@Default] at injection point [[field] @Inject private 
.demo.helpers.jpa.EntityManagerStore.mEntityManagerFactory]. Possible dependencies [[Producer Method [EntityManagerFactory] with qualifiers [@Any @Default] declared as [[method]
 @Produces public ch.demo.helper.EntityManagerFactoryProducer4Test.produceEntityManager()], Producer Method [EntityManagerFactory] with qualifiers [@Any @Default] declared as [[method] 
@Produces public ch.demo.helpers.jpa.EntityManagerFactoryProducer.produceEntityManager()]]]
          at ...

 

 

EntityManagerFactoryProducer produces an EntityManagerFactory and EntityManagerFactoryProducer4Test produces a mock and is annotated as alternative:

 

@Alternative
@ApplicationScoped
public class EntityManagerFactoryProducer4Test {

    /**
     * @return an entity manager factory for test purposes
     */

    @Produces
    public EntityManagerFactory produceEntityManager() {
            return Persistence.createEntityManagerFactory("JEE6Demo-Test-Persistence");    
    }
}

 

 

Using Weld 1.1.5 it did work  but using Weld 1.1.6, I must do the following

 

@Alternative
@ApplicationScoped
public class EntityManagerFactoryProducer4Test {

    /**
     * @return an entity manager factory for test purposes
     */

    @Alternative
    @Produces
    public EntityManagerFactory produceEntityManager() {
            return Persistence.createEntityManagerFactory("JEE6Demo-Test-Persistence");    
    }
}

 

 

 

The problem is that I cannot remove the @Alternative annotation on the class otherwise I cannot declare the alternative in the beans.xml file

 

Is it an expected beavior or a bug?

 

Many thanks in advance

  • 1. Re: Changes in Weld 1.1.6
    Marko Lukša Apprentice

    Yes, this is expected behavior. Please see https://issues.jboss.org/browse/WELD-930

  • 2. Re: Changes in Weld 1.1.6
    Marko Lukša Apprentice

    Oh yes, the spec is a little inconsistent about alternatives and producer methods. I think this is being addressed in CDI 1.1. For now, you have to annotate both the producer method and the declaring bean with @Alternative.

  • 3. Re: Changes in Weld 1.1.6
    Steve Hostettler Newbie

    Hello Marko,

     

    thanks for the answer, I do not really understand how the issue you mentionned relates to the problem.

    Do you know, what did cost this new check to be implemented?

    As there is a simple workaround it is not a big deal, just want to make sure that the issue does not get lost.

  • 4. Re: Changes in Weld 1.1.6
    Marko Lukša Apprentice

    Note Pete's comment in that jira issue:

     

    This looks to me like a bug in Weld (one that crept in from a pre-final spec revision) - that it checks if the declaring bean class is an alternative, and if it is, makes the producer an alternative. Can you file a WELD issue for this? I'll update the quickstart to explicitly make the producer an alternative.

     

    So, in 1.1.6, producer methods and fields are no longer treated as alternatives just because the bean they are declared in is annotated @Alternative. You have to explicitly mark the producer method/field with @Alternative for it to actually be an alternative.

     

    However, in order to enable the alternative producer method, you have to list the method's *declaring bean* in beans.xml (you can't list the producer method itself). And as you've already pointed out, you can't declare a bean as an <alternative> in beans.xml unless the bean is annotated with @Alternative. Therefore, there is no other way than to annotate both the producer method and the declaring bean with @Alternative and then enabling the bean through <alternative> in beans.xml. 

  • 5. Re: Changes in Weld 1.1.6
    Steve Hostettler Newbie

    Hello,

     

    sorry I did not read the issue carefully enough. Thanks for the answer