从应用程序覆盖 getSingletons 方法时 Ejb 查找失败 class

Ejb lookup failing when overriding getSingletons method from Application class

我正在尝试通过自己覆盖应用程序 class 中的 getSingletons 方法来初始化我的 Web 服务资源。我正在使用 Jboss EAP 6.0 应用程序服务器。下面是代码

@ApplicationPath("/rest")
public class MyApplication extends Application {

    private final Set<Object> singletons = new HashSet<Object>();

    public MyApplication() {
        singletons.add(new StudentFacade());
    }

    @Override
    public Set<Object> getSingletons() {

        return null; // Accidentally kept it as null but still my webservice was working. Dont know how it was working.
    }
}

@Path("student")
public class StudentFacade {

    @Resource(lookup = "java:global/ejb-beginner-sessions-ear/ejb-beginner-sessions-impl/StudentServiceBean!com.ejb.beginner.sessions.api.StudentService")
    private StudentService studentService;

    private static final Logger LOGGER = Logger.getLogger(StudentFacade.class.getName());

    @GET
    @Path("list")
    @Produces(MediaType.APPLICATION_JSON)
    public Student getStudents() {

        LOGGER.info("studentService: " + studentService);
        return new Student();
    }
}

然后我意识到 getSingletons 方法 returning null 并且想知道我的网络服务是如何工作的。

我认为因为我没有 returning 单例,所以应用程序服务器正在自行初始化 web 服务。所以我删除了 @Resource 并使用了 @Inject 并且我得到了一个 WELD 异常,说没有找到依赖项。

然后我将其更改为 return 单例,然后 @Resource 没有查找 ejb 并且 studentService 为空。所以我使用 InitialContext 来查找 ejb 并且它起作用了。

try {
            InitialContext initialContext = new InitialContext();
            StudentService studentService = (StudentService) initialContext
                    .lookup("java:global/ejb-beginner-sessions-ear/ejb-beginner-sessions-impl/StudentServiceBean!com.ejb.beginner.sessions.api.StudentService");
            LOGGER.info("studentService1: " + studentService);
        } catch (NamingException e) {
            e.printStackTrace();
        }

谁能告诉我

  1. 尽管我return输入了 null
  2. ,为什么应用程序服务器仍会初始化 Web 服务
  3. 当我 returned null 时,为什么 @Inject 失败而只有 @Resource 在工作。
  4. 当我 returned 单例时,为什么 @Resource 失败并且只有 InitialContext 在工作。

"1. Why did the application server initialize the webservice despite I returned null"

当在查找期间没有返回 classes/objects(或只是空集)时,行为将恢复为 class路径扫描资源和提供者(这在 JAX-RS 规范中有解释) ).一旦 any 资源 class 或提供者从 getClassesgetSingleton 返回,则假定开发人员处理注册,并且 class路径注册已禁用。

"2. When I returned null, why @Inject failed and only @Resource was working."

我没有看到你对 @Inject 的任何论点的成功案例,所以我不会尝试对此发表评论。至于@Resource,这似乎是注入资源的正确方法。启用 class 路径注册后,您不会创建 JAX-RS class,运行时会创建。运行时还处理注入。如果您自己创建实例,运行时将不会尝试注入。

"3. When I returned singletons, why @Resource failed and only InitialContext was working."

以上已部分回答。当您实例化 class 时,不会执行注入。当运行时创建实例时,它会经历注入过程。


需要注意的一件事是,当您实例化时,资源 class 将是一个单例,这意味着每个应用程序只有一个实例。这可能是也可能不是所需的行为。当运行时实例化 class 时,默认情况下它将在请求范围内,这意味着每个请求创建一个实例。同样,可能是也可能不是您想要的。如果你想维护 classpath 注册,并且仍然是单例,那么用 @javax.inject.Singleton 注释资源 class 应该使它成为单例,并且仍然允许注入,因为运行时仍然实例化class。如果您希望 classes 具有请求范围,并希望自己注册所有 classes,请覆盖 public Set<Class<?>> getClasses() 并将您的 classes 添加到集合中。