簡體   English   中英

遷移到JBoss EAP 7后找不到通過注釋選擇CDI實例

[英]CDI instance selection by annotation not found after migration to JBoss EAP 7

我正在和其他人一起在JBoss EAP 7.0.3上運行以下內容來獲得一個耳朵文件:common.jar protocol-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>() {}); 找不到豆。 因此由於某種原因, BeanManager並不認為我的HandshakeMessageMapper是可以提供MessageMapper接口的bean。

從cdi 1.0到cdi 1.1的一個重大變化是如何發現豆。 新的默認設置是僅發現帶注釋的bean。

因此,您有兩種選擇(我知道):

  • @Dependent批注添加到所需的bean。
  • beans.xml中將bean-discovery-mode設置為“ all”
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
   http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
   bean-discovery-mode="all">
</beans>

有關更多詳細信息,您可能需要查看https://blogs.oracle.com/theaquarium/entry/default_cdi_enablement_in_java

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM