1

I have been trying to find out if this is possible but can't get an answer to it.

I have a EJB 3.0 class and its local interface.

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

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

Now, here is the question. I want to define two EJBs with different names that use the same class so that I can inject two different "type" values (defined in ejb-jar.xml).

Then in using that in a different class, for example:

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

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

Thx, Daniel

user585037
  • 21
  • 2
  • Do this thread http://stackoverflow.com/questions/7920123/inject-ejb-bean-based-on-conditions/7923159#7923159 or this one http://stackoverflow.com/questions/7927681/choose-ejb-to-be-injected-without-recompiling/7927814#7927814 is able to solve your problem? – Piotr Nowicki Feb 29 '12 at 16:10

1 Answers1

0

I haven't tried it myself but I would suggest that you try it out and see if it works, I believe it will, however, if it doesn't then you could create a super interface that extends the two local interfaces you want and make your beans implement this new super interface. Then you could call it like

@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;
Marthin
  • 6,413
  • 15
  • 58
  • 95
  • I actually tried and it doesn't work. In spring I would simply instantiate the same class with different bean instance names. – user585037 Jan 25 '12 at 13:39
  • That will still force me to have multiple interfaces. What I'm looking for is to define one interface, one implementing class (that can be customized depending on the parameter values injected), and have multiple instances of it. – user585037 Feb 07 '12 at 20:37