I have a web service which depending on the call executed I need different EJB to be injected.
I can use the @EJB annotation and specify all the EJBs that can be used in the web service.
Example:
@EJB
private EJBType1 ejb1;
@EJB
private EJBType1 ejb1;
@WebMethod
public String readDataFromType1() {
ejb1.call1();
}
@WebMethod
public String readDataFromType2() {
ejb2.call2();
}
What I want to know is when will the EJBs be injected? Only on use of the EJB, or are both injected when any of the web service calls are executed?
I’m worried that should I inject numerous EJBs it will have a negative effect on performance.
EJB is injected before the bean is ready to be used. So both EJBType1 will be injected even though none of their methods are called. But is it any reason to be worried? It depends.
If EJBType1 is a EJB with remote interface then getting reference to such bean might be costly (especially if this bean is a stateful component).
If EJBType1 is a local interface EJB then the overhead connected with injection of such bean is not particularly significant, especially for a stateless component, which can be pooled by application server.