0

再会。我试图使用生产者方法来获取适当的 bean 进行注入。当我打电话时它工作正常

Instance<HttpAmClientTransport> clienttrnsportI = instance.select(HttpAmClientTransport.class);
HttpAmClientTransport clienttrnsport = clienttrnsportI.get();

其中 HttpAmClientTransport.class 是注入 bean 的本地接口,但如果我想在实例上调用迭代器

Iterator<AmClientTransport> aaa = instance.iterator();

这会导致递归工厂类创建(使用执行@Produces 方法)
,这里有讨论,Brian Blonski Java CDI 的回答是:Dynamicly selection an implementation based on qualifier?

但在我的情况下,当我获得迭代器时出现递归,而不是通过 .next() 获得 bean 实例

我也需要你的建议。事实上,我有几个具有自己本地接口的传输 Bean 和几个客户端 bean,我必须在其中注入特定的传输。客户端类型(rest soap 或 db)和传输限定符(安全与否)将在配置文件中给出,并且可能会更改。我认为我可以在带有枚举变量的注释中硬编码一些客户端配置约束和必要的映射,这将包含传输接口类。这些接口(每个客户端 bean 一个)将由传输 bean 实现(并且可能是某些客户端实现了适合每个客户端的所有接口),但这是一些问题。如果我更改,我如何过滤掉在生产者方法中实现该客户端特定接口的所有传输 bean

Instance<HttpAmClientTransport> clienttrnsport = instance.select(HttpAmClientTransport.class);

Instance<RestfulAmClientTransport> clienttrnsport = instance.select(RestfulAmClientTransport.class);

我有 isUnsatisfied() 属性真值(空实例),但如果我将它们更改为

Instance<AmClientTransport> amtranses = instance.select(AmClientTransport.class);

比所有检查都是错误的,但是 get() 方法导致整个生产者类的递归创建我知道我错过了一些关于继承的东西,但是什么?

我会很感激你能给我的任何建议。

代码部分

restfullclient bean

public class  RESTfulAmClientBean 
extends BaseAmClient<RESTfulAmClientBean> 
implements RESTfulAmClient , Serializable{
@Inject
private Logger logger;

private static final long serialVersionUID = 1L;
private AmClientTransport trns;
//private AmClientRequestBuilder rb;
//private AmClientSerializer serializer;
private AmObjectApiSettingsManager amCliSettMgr;


@Inject
@AmClientTransportProducer
private AmClientTransport restfulAmClientTransport;

生产者豆

@Local
public class AmClientTransportProducerImpl {
@Inject
private Logger logger;
@Inject
@Any
private Instance<AmClientTransport> trnsInst;
public AmClientTransportProducerImpl() {
}

@PostConstruct
private void initAmClientTransportProducerImpl(){
    logger.debug("создание экземпляря класса AmClientTransportProducerImpl");

}
@AmClientTransportProducer
@Produces
public AmClientTransport getAmClientTransport(@Any Instance<AmClientTransport> instance , InjectionPoint ip){
    logger.debug("запуск метода getAmClientTransport");
    logger.debug("isAmbig:" + instance.isAmbiguous() + " isUnSatt" + instance.isUnsatisfied());
    Instance<HttpAmClientTransport> clienttrnsport = instance.select(HttpAmClientTransport.class);
    boolean isAmbig = clienttrnsport.isAmbiguous();
    boolean isUnsat = clienttrnsport.isUnsatisfied();
    //Iterator<AmClientTransport> aaa = instance.iterator();
    // Here i have got recursion
    //AmClientTransport trns2 = aaa.next();
    HttpAmClientTransport trns = clienttrnsport.get();
    Instance<AmClientTransport> restfultranses = instance.select(AmClientTransport.class);
    isAmbig = restfultranses.isAmbiguous();
    isUnsat = restfultranses.isUnsatisfied();
    Iterator<AmClientTransport> it = restfultranses.iterator();
    // Here i have got recursion too
    if (
            (!restfultranses.isUnsatisfied())
            ){

    }
    return null;
}

AmClientTransport 接口只是接口

public interface AmClientTransport {}

扩展 AmClientTransport 的客户端特定传输接口

public interface RestfulAmClientTransport extends AmClientTransport {
public void execute();

}

本地特定传输 Bean 接口

@Local
public interface HttpAmClientTransport extends RestfulAmClientTransport{}

特定的传输 Bean 实现

@Stateless(mappedName = "HttpAmClientTransport")
@Local(HttpAmClientTransport.class)
@Default
public class HttpAmClientTransportImpl implements HttpAmClientTransport {

public HttpAmClientTransportImpl() {}

@Override
public void execute() {}
}
4

0 回答 0