Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject one EJB 3.1 into another EJB

I'm developping simple app where one EJB should be injected into another. I'm developping in IDEA Jetbrains IDE. But after i make @EJB annotation in Ejb local statless class my IDE highlight it with error: EJB '' with component interface 'ApplicationController' not found.

Can anyone tell Why?

like image 332
breedish Avatar asked Dec 01 '22 05:12

breedish


2 Answers

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.

like image 108
Pascal Thivent Avatar answered Dec 04 '22 11:12

Pascal Thivent


I believe it's an IntelliJ IDEA bug. This thread solved the problem for me:

adding a EJB Facet (in project structure > modules) helped

like image 42
Matthew Cornell Avatar answered Dec 04 '22 10:12

Matthew Cornell