Java CDI和观察者问题-调用侦听器方法时,依赖项为空
问题:在ContentViewLog上调用listen方法时,日志和实体管理器为空 BlogDetailBean用于JSF2页面的beanJava 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
@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;
}
...
}