Jump to content

Injectar um EJB num bean duma aplicação web


Baderous
 Share

Recommended Posts

Tenho um projecto Maven desenvolvido no Eclipse, o qual faço deploy como sendo um EAR que tem lá dentro um módulo JAR (projecto Java 1.7, EJB 3.1 e JPA 2.0) e um módulo WAR (Java 1.7, Servlet 3.0, JAX-RS 1.1). Estou a fazer deploy para um servidor Weblogic 12.1.1.0. A estrtuura é a seguinte:

backoffice-ear
backoffice-ejb-core-0.0.1-SNAPSHOT.jar
backoffice-ws-0.0.1-SNAPSHOT.war

O projecto WS tem classes que implementam web services REST que invocam os Services e DAOs definidos como Stateless EJB no projecto EJB. Exemplo:

WAR:

@Path("/Events")
@Stateless
public class EventsWS {

@EJB
private EventsService eventsService;

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/searchEvents")
public Response searchEvents() {
   eventsService.searchEvents(null, "S", "TEST", new BigDecimal(1), new BigDecimal(20));
   ...
}
}

JAR:

@Stateless
public class EventsService {

@EJB
private EventsDao dao;

public DtoSearchEvents searchEvents(...) throws ServiceException {
	...
}
}

As injecções funcionam bem dentro do módulo JAR, mas não entre o WAR e o JAR porque o service EJB injectado no EventsWS está sempre a null. Mas se o carregar por JNDI já consigo usá-lo correctamente:

Context context = new InitialContext();
eventsService = (EventsService)context.lookup("java:global/backoffice-ear/backoffice-ejb-core-0.0.1-SNAPSHOT/EventsService");

A questão é que eu quero conseguir injectá-lo sem o carregar por JNDI. Já andei para aqui às voltas com as anotações, tentei usar o atributo mappedName com o path JDNI indicado acima mas sem sucesso (creio que estou a usar o JDNI correcto). Deixo aqui o meu web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>backoffice-ws</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>backoffice-ws-servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
 <param-name>com.sun.jersey.config.property.packages</param-name>
 <param-value>xxx.yyy.zzz.backoffice.ws</param-value>
</init-param>
<init-param>
 <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
 <param-value>true</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>backoffice-ws-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Edited by Baderous
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.