Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java CDI和观察者问题-调用侦听器方法时,依赖项为空_Java_Jsf 2_Cdi_Ejb 3.1 - Fatal编程技术网

Java CDI和观察者问题-调用侦听器方法时,依赖项为空

Java CDI和观察者问题-调用侦听器方法时,依赖项为空,java,jsf-2,cdi,ejb-3.1,Java,Jsf 2,Cdi,Ejb 3.1,问题:在ContentViewLog上调用listen方法时,日志和实体管理器为空 BlogDetailBean用于JSF2页面的bean @Named @RequestScoped public class BlogDetailBean { @Inject private BlogService blogService; @Inject Event<ContentViewEvent> blogViewEvent; ... pu

问题:在ContentViewLog上调用listen方法时,日志和实体管理器为空

BlogDetailBean用于JSF2页面的bean

@Named
@RequestScoped
public class BlogDetailBean {

    @Inject
    private BlogService blogService;

    @Inject
    Event<ContentViewEvent> blogViewEvent;

    ...

    public String loadEntry(){
      this.blogViewEvent.fire(new ContentViewEvent(this.entry));
    }

    ...

}
另一方面,尤其令人困惑的是,ContentViewLog的其他方法(如GetTotalView)在从其他bean使用时可以工作,尽管在这些情况下,我没有使用CDI事件


仅供参考-上面没有显示的两个bean使用@products来提供Logger和EntityManager实例。

只是一个简单的猜测,因为我同意我们需要知道您使用的是什么CDI和容器,但是您是否尝试过将observer方法公开而不是私有?我感觉代理在这种情况下无法正常工作

你有任何错误信息吗?您能发布一些堆栈跟踪吗?这是焊接还是其他impl?根据CDI规范,私有方法是完全可以接受的
@Stateless
@Named
public class ContentViewLog {

    @Inject
    private Logger log;

    @Inject
    @DataRepository
    private EntityManager em;


    private void listen(@Observes final ContentViewEvent e) {
        this.log.info("Content View Event: " + e.toString());
        final LoggedContentView lcv = new LoggedContentView(e);
        this.em.persist(lcv);
    }

    public Long getTotalViews() {
        final Long result = (Long) this.em.createNamedQuery(
            "loggedContentView.countAll").getSingleResult();
        return result;
}

    ...

}