【发布时间】:2017-06-01 15:38:26
【问题描述】:
我有一个在 JBoss EAP 7.0.3 上运行的带有(以及其他)以下内容的 ear 文件: common.jar 协议-v4.jar
在 common.jar 我有以下代码(部分):
MessageServiceImpl.java:
@Any
@Inject
private Instance<MessageMapper<M>> mappers; // M -> SimpleMessage
// ...
MessageMapper<M> mapper = findInstance(mappers, new MapperType.Literal(messageType, parameters.getProtocolVersion()), "message mapper");
// inside findInstance:
Instance<T> candidate = instance.select(annotationLiteral);
首先结果:candidate 上面的 isUnsatisfied() 为 true。
现在实现在protocol-v4.jar中:
@MapperType(messageType = MessageType.HANDSHAKE, version = Protocol.VERSION)
public class HandshakeMessageMapper extends AbstractPositiveMessageMapper {
AbstractPositiveMessageMapper 在其链中某处实现MessageMapper<SimpleMessage>
我以前在 JBoss EAP 6.4.10 中使用它,但现在使用 JBoss EAP 7.0.3 突然不满意了。我错过了什么?这是一个错误,它曾经工作过吗?提前谢谢你。
编辑
我已经开始使用 BeanManager 来进一步探索正在发生的事情。这是instance.select之前的:
Set<Bean<?>> beans = beanManager.getBeans(Object.class, new AnnotationLiteral<Any>() {});
for(Bean<?> bean : beans) {
if(bean.getBeanClass().getName().contains("v104.mapper.Handshake")) {
for(Annotation annotation : bean.getQualifiers()) {
if(annotation.annotationType() == MapperType.class) {
MapperType mapper = (MapperType) annotation;
logger.info("Type: {}, Version: {}", mapper.messageType(), mapper.version());
} else {
logger.info("Qualifier: {}", annotation.annotationType());
}
}
logger.info("Bean: " + bean);
}
}
这有效,并将我的 HandshakeMessageMapper 显示为具有正确限定符(@Any 和 @MapperType(value & protocol))的 bean。
但是,如果我将 getBeans 更改为:beanManager.getBeans(MessageMapper.class, new AnnotationLiteral<Any>() {});,则找不到任何 bean。所以出于某种原因,BeanManager 不认为我的HandshakeMessageMapper 是一个可以提供接口MessageMapper 的bean
【问题讨论】:
-
嗨,我在 Jakarta 8 (CDI 2.0) 中遇到了同样的问题,就像你一样,我有所有的注释(也尝试使用 @ApplicationScoped 和 @Typed)。你解决了吗?谢谢!