如何注入两个相同类型的EJB? [英] How can I inject two EJBs of same type?

查看:303
本文介绍了如何注入两个相同类型的EJB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用不同的注释注入两个相同类型的EJB。
但注入的实例是相同的。

  @Path(/ some)
public class SomeResource {

@Inject
@SomePostConstructionAnnotationForSomeService(this)
private SomeService s1;

@Inject
@SomePostConstructionAnnotationForSomeService(that)
private SomeService s2;
}

SomeService类看起来像这样。

  public SomeService {

@PostConstruct
private void construct(){
//找到注释并执行其他操作。
}

@Inject
私人注入点注入点;
}

问题是只有 s1 被调用, s2 等于 s1

解决方案

如果 SomeService CDI bean,它的范围是 @Dependent 或没有范围(也意味着 @Dependent 注入另一个bean)。它有什么范围?如果 SomeService EJB bean not CDI 这不会工作。



更新:
您可以将SomeService移动到基类,并通过扩展此类并注入来创建两个不同的EJB bean这些EJB。


I'm trying to inject two EJBs of same type with different annotation. But the instances injected are same.

@Path("/some")
public class SomeResource {

    @Inject
    @SomePostConstructionAnnotationForSomeService("this")
    private SomeService s1;

    @Inject
    @SomePostConstructionAnnotationForSomeService("that")
    private SomeService s2;
}

SomeService class looks like this.

public SomeService {

    @PostConstruct
    private void constructed() {
        // find the annotation and do something else.
    }

    @Inject
    private InjectionPoint injectionPoint;
}

The problem is that only s1 is invoked and s2 is equals s1.

解决方案

This will work if SomeService is CDI bean and its scope is @Dependent or no scope (that also means @Dependent when injected into another bean). What scope does it have? If SomeService is EJB bean not CDI this won't work.

UPDATE: You could move SomeService to a base class and create two different EJB beans by extending this class and inject these EJBs.

这篇关于如何注入两个相同类型的EJB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆