C
C
cyberorg2013-11-02 23:39:23
Java
cyberorg, 2013-11-02 23:39:23

Interaction between two bean modules within the same application?

Hello Habralyudi!
Trying to get an EJB from another module throws a NullPointerException.
I will give an example to make it clearer.
The general structure of the application is:
EAR
|
- core.jar (EJB module with core beans) - application core
|
- app.jar (another EJB module with beans) - business logic
|
- web.war (servlets)
The following EJB is declared in code.jar:

@LocalBean
@Singleton
@Startup
public class AppInfo(){
   private int counter;
   
   public void incCounter(){
     counter++;
  }
  public int getCounterValue(){
    return counter;
  }

}

The app.jar module has a Stateless bean that tries to read the counter'a value.
@Stateless
public class SomeBean{
   @EJB private AppInfo appinfo;

    public void run(){
       int counter = appInfo.getCounterValue(); // здесь метод падает с NPE 
       System.out.println("Counter value is: "+counter);
   }   
}

The web.war module has a servlet that increments a counter on every request:
public class MyServlet extends HttpServlet{
        @EJB private AppInfo appInfo;
        protected void doPost(params){
              appInfo.incCounter();
              ...
              other code
       }
  }

When debugging, you can see:
Everything works in the servlet: when you enter doPost(), the appInfo value is a Proxy object for the AppInfo EJB.
An NPE occurs in SomeBean: when entering run(), the value of appInfo is null.
How to properly inject EJB from Core module into EJB from App module?
PS Deploying EAR'nick on JBoss 6.1 EAP
PSS I tried it via the Remote interface: it doesn't work (appInfo is null+NPE in the method)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
KingOfNothing, 2013-11-03
@KingOfNothing

Well, it's probably not only necessary to declare @EJB private AppInfo appInfo; But also create a bean?
like appInfo = new AppInfo();
Or you don't want to manually create?

E
Evgin, 2013-11-03
@Evgin

The option proposed above with the creation of an AppInfo instance through new is incorrect according to the ee ideology. Servlet injection should work. Check the documentation for the specific container version. There are suspicions that it is necessary to inject into the servlet not the AppInfo class, but the local interface.

Z
zarc69, 2013-11-03
@zarc69

In my project, injection works through @Localthe interface.

V
Vladimir Smirnov, 2013-11-04
@bobzer

You didn't show how SomeBean is created. What I mean is that the container injects into the beans that it creates. If you created SomeBean with new, then there will be no injections into it.
You can also dig in the direction of class loading, which is significantly redesigned in fresh JBoss compared to previous versions. Try, for example, specifying <ear-subdeployments-isolated>false</ear-subdeployments-isolated> in jboss-deployment-structure.xml. In the end, combine core.jar and app.jar into one jar (stupidly using WinRar, if it takes a long time to redo build scripts) and see if there will be an injection. If it is, then 99% that you need to pick the class loading. If not, then it probably has nothing to do with it, and you need to think further.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question