简体   繁体   中英

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?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM