Rustam
Rustam

Reputation: 1407

WELD-001409: Ambiguous dependencies for type DataProvider with qualifiers @Default

All components (jars, wars) has beans.xml with bean-discovery-mode="annotated".

DataProvider - interface, all classes that implements that interface are not annotated. Exists only one producer with 2 methods:

@ApplicationScoped
public class DataProviderProducer {

    @Resource(lookup = JndiNames.DS_OLTP)
    private DataSource oltpDataSource;

    @Resource(lookup = JndiNames.DS_RD)
    private DataSource rdDataSource;

    @Produces
    @OLTP
    public DataProvider createOltpDataProvider() {
        return new JDBCDataProvider(oltpDataSource);
    }

    @Produces
    @RD
    public DataProvider createRdDataProvider() {
        return new JDBCDataProvider(rdDataSource);
    }

}

When injecting like this:

@Inject
@OLTP
private DataProvider dp;

we get the following error:

org.jboss.weld.exceptions.DeploymentException: WELD-001409: Ambiguous dependencies for type DataProvider with qualifiers @Default
  at injection point [BackedAnnotatedField] @OLTP @Inject private mypackage.MyBean.dp
  at mypackage.MyBean.dp(MyBean.java:0)
  Possible dependencies: 
  - Producer Method [DataProvider] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces @RD public mypackage.injection.DataProviderProducer.createRdDataProvider()],
  - Producer Method [DataProvider] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces @OLTP public mypackage.injection.DataProviderProducer.createOltpDataProvider()]

"Ambiguous dependencies for type DataProvider with qualifiers @Default" - injection only with @RD or @OLTP annotaions, I'm not using @Default anywhere. 10 times rechecked everything in code, I don't know what to do.

Used WildFly 18, OpenJDK 13.

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
public @interface OLTP {

}

//

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
public @interface RD {

}

Upvotes: 1

Views: 946

Answers (2)

mtj
mtj

Reputation: 3554

Well, you solved your problem yourself, but the background is, that modules and applications in wildfly/jboss are loaded by different classloaders.

Therefore, the annotation-classes are not the same in the somewhat convoluted world of java classloading, e.g. OLTP.class loaded by classloader 1 and OLTP.class loaded by classloader 2 are different classes.

Upvotes: 1

Rustam
Rustam

Reputation: 1407

Problem solved by moving annotations OLTP and RD from one library (in WILDFLY_HOME/modules) to another (in deployment ear file).

Upvotes: 1

Related Questions