问题:在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事件)。
仅供参考-上面没有显示的两个bean使用@Produces来提供记录器和EntityManager实例。
发布于 2011-08-28 10:14:47
我同意我们需要知道您使用的是什么CDI和容器,但您是否尝试过将观察者方法设置为公共而不是私有?我有一种感觉,在这种情况下,代理不会正常工作。
https://stackoverflow.com/questions/7132801
复制相似问题