一段时间以来,我一直在努力解决这个问题,并认为我最终会寻求帮助/想法,说明为什么我的 EJB 没有被正确注入。我正在使用带有 Apache Wink 的带有 EJB 3 和 WAS 社区版的 Java 6,并在尝试访问 ITestService 时不断获得 NPE。如果我尝试在上下文中查找 bean,它会抛出 javax.naming.NameNotFoundException。有什么我遗漏的/否则我必须做些什么才能将 bean 添加到上下文中?
应用类
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet<Class<?>>();
resources.add( TestResource.class );
return resources;
}
}
bean 的接口
@Local
public interface ITestService {
public String test();
}
bean的实现类
@Singleton
public class TestService implements ITestService {
@Override
public String test() {
return "Hello World!";
}
}
JAX-RS 资源
@Path("/test")
@Produces(MediaType.TEXT_PLAIN)
@Stateless
public class TestResource {
@EJB
private ITestService service;
@GET
public Response test() {
String value = service.test();
return Response.ok( value ).build();
}
}
web.xml
<web-app version="2.5" 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_2_5.xsd">
<description>My Web Application</description>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.gordysc.myfit.server.servlets.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
</web-app>
geronimo-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1"
xmlns:naming="http://geronimo.apache.org/xml/ns/naming-1.2"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
<context-root>/myfit</context-root>
</web-app>