我在项目中使用了两个面向消息传递的中间件。 RabbitMQ和Apache Kafka。我有一个由ConsumerRabbitMQ和ConsumerKafka实现的消费者接口IConsume。在启动过程中,通过某些条件,我使用@Produces批注选择了要注入的Interface Bean的实现,但这给了我这个错误。
例外1:
org.jboss.weld.exceptions.DeploymentException: WELD-001409: Ambiguous dependencies for type IConsume with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private com.mycompany.chatapp.startup.RunConsumers.ct
at com.mycompany.chatapp.startup.RunConsumers.ct(RunConsumers.java:0)
Possible dependencies:
- Session bean [class com.mycompany.chatapp.messagegateway.ConsumerRabbitMQ with qualifiers [@Any @Default]; local interfaces are [IConsume],
- Producer Method [IConsume] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces public com.mycompany.chatapp.startup.MOMConfigBean.produceIConsume()],
- Session bean [class com.mycompany.chatapp.messagegateway.ConsumerKafka with qualifiers [@Any @Default]; local interfaces are [IConsume]
@Default和@Alternative可以使用,但是我希望通过检查正在运行的中间件来进行选择。
查找有效,我也尝试了beanName。我认为问题出在@Produces,但我找不到什么。
import javax.enterprise.inject.Produces;
@Singleton
@Startup
public class MOMConfigBean {
private String mom;
@PostConstruct
public void init() {
mom = "Kafka";
}
@EJB(lookup = "java:global/Chatapp/ConsumerKafka!com.mycompany.chatapp.messagegateway.IConsume")
IConsume kafkaConsumer;
@EJB(lookup = "java:global/Chatapp/ConsumerRabbitMQ!com.mycompany.chatapp.messagegateway.IConsume")
IConsume rabbitConsumer;
@Produces
public IConsume produceIConsume() {
if ("Kafka".equals(mom)) {
return kafkaConsumer;
} else {
return rabbitConsumer;
}
}
public interface IConsume {
// some code
}
@Stateless
public class ConsumerKafka implements IConsume{
// some code
}
@Stateless
public class ConsumerRabbitMQ implements IConsume {
// some code
}
public class runConsumers{
@Inject
private IConsume ct;
}
答案 0 :(得分:0)
您有IConsume
实例的三个模糊来源:
ConsumerKafka
EJB ConsumerRabbitMQ
EJB @Produces public IConsume produceIConsume()
方法。您需要使用限定符消除IConsume
实例的来源的歧义。
该限定符如下所示:
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD})
public @interface ConditionalMom {
}
然后使生产者合格:
@Produces
@ConditionalMom
public IConsume produceIConsume() {
if ("Kafka".equals(mom)) {
return kafkaConsumer;
} else {
return rabbitConsumer;
}
}
和注射部位:
public class runConsumers{
@Inject
@ConditionalMom
private IConsume ct;
}
现在,您拥有@ConditionalMom IConsume
个实例的单一来源,因此不再歧义。
当您开始进一步利用CDI功能时,您会发现在各地都将使用限定符。