Jsf 用户类实例化后如何调用CDI?
我有一个类似这样的类:Jsf 用户类实例化后如何调用CDI?,jsf,jsf-2,cdi,Jsf,Jsf 2,Cdi,我有一个类似这样的类: @Named public class TableView { @PersistenceContext protected EntityManager em; @Resource protected UserTransaction utx; @Inject private TableView view; 当然,在构建bean的过程中,我可以得到如下实例: @Named public class TableView {
@Named
public class TableView {
@PersistenceContext protected EntityManager em;
@Resource protected UserTransaction utx;
@Inject private TableView view;
当然,在构建bean的过程中,我可以得到如下实例:
@Named
public class TableView {
@PersistenceContext protected EntityManager em;
@Resource protected UserTransaction utx;
@Inject private TableView view;
我相信是CDI负责填写EntityManager和UserTransaction。然而,在我的用户类被实例化之后,我有时需要TableView的另一个实例,那么如何获得它呢?显然
TableView anotherView = new TableView();
由于em和utx将为空,因此无法工作。那么,如何在执行注入的情况下获得新的工作实例呢?接口应该满足您的需要:
Instance<TableView> tableViewInstance;
TableView anotherView = tableViewInstance.get();
实例tableViewInstance;
TableView anotherView=tableViewInstance.get();
但如评论中所述,您的视图不应了解交易和实体经理。此链接可能会解决您的问题@Ankit这很有趣,但不是很成功。我正在寻找一个运行时API来实例化bean.Interface和method
get()
<代码>实例tableViewInstance;TableView newInstance=tableViewInstance.get()代码>@Geinmachi这似乎就是我要找的。我试试看。如果你想把它作为一个答案发布,我会在验证后标记它。什么“用户类”。。。但是,在视图中需要EM和UT这一事实向我表明了应用程序中更基本的设计问题。我的TableView对象是有状态DAO,提供了从数据库中获取记录列表的方法。状态包括第一条记录、查询大小、筛选器、排序等。在不访问EntityManager的情况下,如何构建标准查询并运行它们?关于JPA和CDI是否有一些规则我不知道?你说的“有状态的DAO”是什么意思?EJB@Stateful
?您的TableView有@命名的
注释,所以它不是绑定到xhtml
页面吗?如果是这样的话,那么它就是表示层,您在TableView
(事务和EM)中所做的事情应该在业务逻辑层(EJB@Stateless
或@Stateful
)中通过依赖注入(@Inject
或@EJB
)通过委托完成。这是JSF web应用程序,而不是EJB。中的支持bean被注入到JSFServlet的上下文中,它反过来需要注入各种类型的数据访问对象。