2012-01-24 47 views
1

我一直在試圖找出這是否可能,但無法得到答案。EJB 3.0多個實例

我有一個EJB 3.0類和它的本地接口。

@Local 
public interface MyService { 
    public String foo(); 
} 

@Stateless 
public class MyServiceBean implements MyService { 
    @Resource(name="type") private String type; 
    public String foo() { return type; } 
} 

現在,這是問題。我想定義兩個使用相同類的不同名稱的EJB,以便我可以注入兩個不同的「類型」值(在ejb-jar.xml中定義)。

然後使用在不同的類,例如:

@EJB(mappedName="MyServiceBeanA") 
private MyService myServiceBeanA; 

@EJB(mappedName="MyServiceBeanB") 
private MyService myServiceBeanB; 

THX, 丹尼爾

+0

做這個線程http://stackoverflow.com/questions/7920123/inject-ejb-bean-based-on-conditions/7923159#7923159或這一個HTTP://計算器.com/questions/7927681/choose-ejb -in-inject-without-recompiling/7927814#7927814能夠解決你的問題嗎? –

回答

0

我還沒有嘗試過自己,但我建議你嘗試一下,看看是否它工作,但我相信它會,但是,如果它不,那麼你可以創建一個超級接口,擴展你想要的兩個本地接口,並讓你的bean實現這個新的超級接口。然後,你可以這樣調用它

@Local 
public interface MyService1 { 
    public String foo(); 
} 

@Local 
public interface MyService2 { 
    public String foo(); 
} 



public interface SuperInterface extends MyService1, MyService2{ 

} 

@Stateless 
public class MyServiceBean implements SuperInterface { 
    @Resource(name="type") private String type; 
    public String foo() { return type; } 
} 


@EJB(mappedName="MyServiceBeanA") 
private SuperInterface myServiceBeanA; 

@EJB(mappedName="MyServiceBeanB") 
private SuperInterface myServiceBeanB; 
+0

我實際上試過了,它不起作用。在春天,我只是用不同的bean實例名稱實例化同一個類。 – user585037

+0

這仍然會迫使我有多個接口。我正在尋找的是定義一個接口,一個實現類(可以根據注入的參數值定製),並有多個實例。 – user585037