How can I inject two EJBs of same type?

∥☆過路亽.° 提交于 2019-12-12 03:59:30

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/32909962/how-can-i-inject-two-ejbs-of-same-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!