Answer the question
In order to leave comments, you need to log in
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;
}
}
@Stateless
public class SomeBean{
@EJB private AppInfo appinfo;
public void run(){
int counter = appInfo.getCounterValue(); // здесь метод падает с NPE
System.out.println("Counter value is: "+counter);
}
}
public class MyServlet extends HttpServlet{
@EJB private AppInfo appInfo;
protected void doPost(params){
appInfo.incCounter();
...
other code
}
}
Answer the question
In order to leave comments, you need to log in
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?
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.
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 questionAsk a Question
731 491 924 answers to any question