【问题标题】:EJB as an Interface is not injected不注入 EJB 作为接口
【发布时间】:2016-10-04 09:29:04
【问题描述】:

我定义了一个接口。

@Local
public interface MessageService {
}

还有一个实现。

@Stateless
public class MessageServiceSome implements MessageService {
}

当我尝试将它注入我的资源类时,我得到了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 类实际上是一个子资源。我不知道有什么区别。

这个问题有两个非常好的线程。

  1. https://stackoverflow.com/a/36291890/330457
  2. https://stackoverflow.com/a/24670218/330457

一个使用ResourceContext,另一个使用Inject

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

【问题讨论】:

    标签: dependency-injection ejb


    【解决方案1】:

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

    1. 如果您的项目/容器启用了 CDI,则仅保留 @Inject

      @注入 私有 MessageService 消息服务;

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

      @EJB 私有 MessageService 消息服务;

    两者中的一个应该可以解决问题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-12
      • 2017-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多