2016-10-04 66 views
1

我定义了一个接口。EJB作为接口不被注入

@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; 
} 

我该如何解决这个问题?

UPDATE

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

MessagesResource类实际上是一个子资源。我不知道区别。

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

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

一种是使用ResourceContext和另一种是采用Inject

这两个线程都说他们工作,但我只成功@Inject

回答

2

利用所提供的信息很少,你有可能有两个快速选项,你可以尝试:

  1. 休假@Inject而已,如果你的项目/容器CDI启用

    @Inject 私人MessageService messageService;

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

    @EJB private MessageService messageService;

就应该解决两个问题。

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