用于数据模型的CDI生产者方法

我希望能够@Inject支持RichFaces 4 ExtendedDataTable的数据模型,但它需要EntityManager来完成它的工作。 当然,EntityManager的查询需要知道Class,我宁愿不将它传递给方法调用(在这种情况下,我的代码不调用这些方法); 理想情况下,它将在构造函数中。

像这样的东西:

public class DataModel { @Inject private EntityManager em; private Class entityClass; public DataModel(Class entityClass) { this.entityClass = entityClass; } //Sample method - this class will handle much more complex queries public T findEntity(String key) { return em.find(entityClass, key); } 

是否可以创建一个CDI @Producer,可用于将此DataModel注入我的支持bean? 我想过制作一个限定符,所以你可以做类似的事情

 @Inject @JType(value = MyEntity.class) DataModel dataModel; 

但这看起来很笨拙,并且还需要我的@Producer调用new() – 我认为不允许将EntityManager注入到DataModel中。 此外,我不确定您将如何要求开发人员添加限定符。

或许有更好的方法来解决这个问题,我错过了一些东西?

我使用seam3中的seam-persistence模块执行此操作。 :

制片人:

 public class EntityManagerProducer { @Produces @ExtensionManaged @ConversationScoped @PersistenceUnit(unitName = "yourUnitName") private EntityManagerFactory emf; } 

然后你可以@Inject实体管理器。

否则, DeltaSpike项目似乎很有希望(从未使用过)