问题:在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(侦听ContentViewEvents的bean)
@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;
}
...
}
另外,特别令人困惑的是,ContentViewLog的其他方法(如getTotalViews)在从其他bean使用时可以工作(但在这些情况下,我没有使用CDI事件。)
仅供参考 - 上面未显示2个使用@Produces提供Logger和EntityManager实例的bean。
答案 0 :(得分:0)
只是在黑暗中拍摄,因为我同意我们需要知道您正在使用的CDI和容器,但是您是否尝试过将观察者方法公开而不是私有?我觉得代理在这种情况下无法正常工作。