Injection of an EJB reference into another EJB can be done using the @EJB
annotation. Here is an example taken from Injection of other EJBs Example from the OpenEJB documentation:
The Code
In this example we develop two simple
session stateless beans (DataReader
and DataStore), and show how we can
use the @EJB annotation in one of
these beans to get the reference to
the other session bean
DataStore session bean
Bean
@Stateless
public class DataStoreImpl implements DataStoreLocal, DataStoreRemote{
public String getData() {
return "42";
}
}
Local business interface
@Local
public interface DataStoreLocal {
public String getData();
}
Remote business interface
@Remote
public interface DataStoreRemote {
public String getData();
}
DataReader session bean
Bean
@Stateless
public class DataReaderImpl implements DataReaderLocal, DataReaderRemote {
@EJB private DataStoreRemote dataStoreRemote;
@EJB private DataStoreLocal dataStoreLocal;
public String readDataFromLocalStore() {
return "LOCAL:"+dataStoreLocal.getData();
}
public String readDataFromRemoteStore() {
return "REMOTE:"+dataStoreRemote.getData();
}
}
Note the usage of the @EJB
annotation
on the DataStoreRemote and
DataStoreLocal fields. This is the
minimum required for EJB ref
resolution. If you have two beans that
implement the same business
interfaces, you'll want to the
beanName attribute as follows:
@EJB(beanName = "DataStoreImpl")
private DataStoreRemote dataStoreRemote;
@EJB(beanName = "DataStoreImpl")
private DataStoreLocal dataStoreLocal;
Local business interface
@Local
public interface DataReaderLocal {
public String readDataFromLocalStore();
public String readDataFromRemoteStore();
}
(The remote business interface is not
shown for the sake of brevity).
If it doesn't work as expected, maybe show some code.