不注入 EJB 作为接口

EJB as an Interface is not injected

我定义了一个接口。

@Local
public interface MessageService {
}

和一个实现。

@Stateless
public class MessageServiceSome implements MessageService {
}

当我试图将它注入我的资源 class 时,我得到了 null

//@Path("/messages") // I'M NOT GOING TO MAKE THIS STATELESS!!!
public class MessagesResource {

    // none of follwoing options works
    // leaves the field null
    @Inject
    //@EJB
    //@EJB(beanName = "MessageServiceSome")
    private MessageService messageService;
}

我该如何解决这个问题?

更新

我想我不得不承认我的问题不够好。

MessagesResource class 实际上是一个子资源。我不知道有什么区别。

这个问题有两个很好的线程。

一个正在使用 ResourceContext and the other is using Inject

两个线程都说它们有效,但我只成功了 @Inject

由于提供的信息很少,您可能有两个可以尝试的快速选项:

  1. 仅保留 @Inject,如果您的 project/container 启用了 CDI

    @注入 private MessageService messageService;

  2. 只留下@EJB,你真的需要beanName吗?

    @EJB private MessageService messageService;

其中一个应该可以解决问题。

[更新] 否则查看应用服务器启动日志,查看是否已部署 bean。