将singleton注入无状态bean是null

时间:2015-09-30 18:14:18

标签: null ejb javabeans code-injection stateless

我有一个Singleton EJB,我正在尝试注入无状态bean,它也是一个WS端点(Jersey)。

注入的单例始终为空。

这个项目被打包成一场战争,部署在Wildfly 9中。

我确实在WEB-INF中放置了一个beans.xml。

web.xml中的web-app版本是3.0:

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>false</param-value>
  </context-param>
  <context-param>
    <param-name>resteasy.scan.providers</param-name>
    <param-value>false</param-value>
  </context-param>
  <context-param>
    <param-name>resteasy.scan.resources</param-name>
    <param-value>false</param-value>
  </context-param>
</web-app>

会话bean:

@LocalBean
@Stateless
@Path(UserRestService.RESOURCE_PATH)
public class UserRestService {

public static final String RESOURCE_PATH = "user";

@EJB
private SingletonEJB singleton;
...

@GET
@Path("/test")
public Response test() {

    if(singleton==null) {
        System.out.println("Singleton is null");
    }
    return Response.ok().build();
}

的Singleton:

@Startup
@LocalBean
@Singleton
public class SingletonEJB {

public SingletonEJB() {     
    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> constructor");
}

在wildfly日志中,我可以看到两个bean的JNDI名称:

16:57:30,176 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread1-4) JNDI bindings for session bean named UserRestService in deployment unit deployment "rest.war" are as follows:

    java:global/rest/UserRestService!com.test.rest.UserRestService
    java:app/rest/UserRestService!com.test.rest.UserRestService
    java:module/UserRestService!com.test.rest.UserRestService
    java:global/rest/UserRestService
    java:app/rest/UserRestService
    java:module/UserRestService

16:57:30,176 INFO  org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor](MSC service thread 1-4) JNDI bindings for session bean named SingletonEJB in deployment unit deployment "rest.war" are as follows:

    java:global/rest/SingletonEJB!com.test.rest.singletons.SingletonEJB
    java:app/rest/SingletonEJB!com.test.rest.singletons.SingletonEJB
    java:module/SingletonEJB!com.test.rest.singletons.SingletonEJB
    java:global/rest/SingletonEJB
    java:app/rest/SingletonEJB
    java:module/SingletonEJB

我可以看到单例的构造函数已运行。 单例不实现任何接口,但注释@LocalBean

我已经阅读了无数帖子,看过this example但我还没弄明白......

LE :添加了Eclipse可导入的简化演示here

0 个答案:

没有答案