How to use EJB 3.1 DI in Servlet ? (Could not inject session bean by @EJB from web application)
- by kislo_metal
I am tying to merging web application(gwt, jpa) to an separate 2 application(business login in ejb/jpa and web client in gwt). Currently i can`t inject my beans from web application (simple servlet)
I am using glassfish v3.
module limbo(ejb jar) is in dependency of module lust (war).
If I use lust with compiler output of limbo everything work perfect (if ejb in web application and the are deploying together as one application).
Have I messed some container configuration ?
Here is my steps:
I have some limbo.jar (ejb-jar) deployed to ejb container. I do not use any ejb-jar.xml, only annotations.
package ua.co.inferno.limbo.persistence.beans;
import javax.ejb.Remote;
@Remote
public interface IPersistentServiceRemote {
ArrayList<String> getTerminalACPList();
ArrayList<String> getBoxACPList();
ArrayList<String> getCNPList();
ArrayList<String> getCNSList();
String getProductNamebyID(int boxid);
ArrayList<String> getRegionsList(String lang);
long getSequence();
void persistEntity (Object ent);
}
package ua.co.inferno.limbo.persistence.beans;
import ua.co.inferno.limbo.persistence.entitis.EsetChSchemaEntity;
import ua.co.inferno.limbo.persistence.entitis.EsetKeyActionsEntity;
@Local
public interface IPersistentService {
ArrayList<String> getTerminalACPList();
ArrayList<String> getBoxACPList();
ArrayList<String> getCNPList();
ArrayList<String> getCNSList();
String getProductNamebyID(int boxid);
ArrayList<String> getRegionsList(String lang);
long getSequence();
long persistPurchaseBox(EsetRegPurchaserEntity rp);
void removePurchaseTempBox(EsetRegPurchaserTempEntity rpt);
EsetRegionsEntity getRegionsById(long rid);
void persistEntity (Object ent);
}
package ua.co.inferno.limbo.persistence.beans;
import ua.co.inferno.limbo.persistence.entitis.EsetChSchemaEntity;
import ua.co.inferno.limbo.persistence.entitis.EsetKeyActionsEntity;
import ua.co.inferno.limbo.persistence.entitis.EsetRegBoxesEntity;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless(name = "PersistentService")
public class PersistentServiceEJB
implements
IPersistentService, IPersistentServiceRemote{
@PersistenceContext(unitName = "Limbo")
EntityManager em;
public PersistentServiceEJB() {
}
.........
}
Than i trying to use PersistentService session bean(included in limbo.jar) from web application in lust.war (the limbo.jar & lust.war is not in ear)
package ua.co.lust;
import ua.co.inferno.limbo.persistence.beans.IPersistentService;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "ServletTest",
urlPatterns = {"/"})
public class ServletTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
service(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
service(request, response);
}
@EJB
private IPersistentService pService;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String hi = pService.getCNPList().toString();
System.out.println("testBean.hello method returned: " + hi);
System.out.println("In MyServlet::init()");
System.out.println("all regions" + pService.getRegionsList("ua"));
System.out.println("all regions" + pService.getBoxACPList());
}
}
web.xm
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>ServletTest</servlet-name>
<servlet-class>ua.co.lust.ServletTest</servlet-class>
</servlet>
</web-app>
When servelt is loading i ge 404 eror (The requested resource () is not available.)
And errors in logs :
global
Log Level
SEVERE
Logger
global
Name-Value Pairs
{_ThreadName=Thread-1, _ThreadID=31}
Record Number
1421
Message ID
Complete Message
Class [ Lua/co/inferno/limbo/persistence/beans/IPersistentService; ] not found. Error while loading [ class ua.co.lust.ServletTest ]
javax.enterprise.system.tools.deployment.org.glassfish.deployment.common
Log Level
WARNING
Logger
javax.enterprise.system.tools.deployment.org.glassfish.deployment.common
Name-Value Pairs
{_ThreadName=Thread-1, _ThreadID=31}
Record Number
1422
Message ID
Error in annotation processing
Complete Message
java.lang.NoClassDefFoundError: Lua/co/inferno/limbo/persistence/beans/IPersistentService;
ejb jar was deployed with this info log :
Log Level
INFO
Logger
javax.enterprise.system.container.ejb.com.sun.ejb.containers
Name-Value Pairs
{_ThreadName=Thread-1, _ThreadID=26}
Record Number
1436
Message ID
Glassfish-specific (Non-portable) JNDI names for EJB PersistentService
Complete Message
[ua.co.inferno.limbo.persistence.beans.IPersistentServiceRemote#ua.co.inferno.limbo.persistence.beans.IPersistentServiceRemote, ua.co.inferno.limbo.persistence.beans.IPersistentServiceRemote]
Log Level
INFO
Logger
javax.enterprise.system.tools.admin.org.glassfish.deployment.admin
Name-Value Pairs
{_ThreadName=Thread-1, _ThreadID=26}
Record Number
1445
Message ID
Complete Message
limbo was successfully deployed in 610 milliseconds.
Do i nee to add some additional configuration in a case of injections from others application?
Some ideas?