用于数据模型的CDI生产者方法 [英] CDI producer method for data model

查看:120
本文介绍了用于数据模型的CDI生产者方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to be able to @Inject a data model backing a RichFaces 4 ExtendedDataTable, but it requires an EntityManager to do its work. The EntityManager's queries need to know the Class, of course, and I'd rather not pass that into method calls (in this case the methods are not called by my code); ideally it would be in the constructor.

这样的事情:

public class DataModel<T> {
    @Inject private EntityManager em;
    private Class<T> entityClass;

    public DataModel(Class<T> 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?我已经考虑过制作一个限定符,所以你可以做类似的事情

Is it possible to create a CDI @Producer that can be used to inject this DataModel into my backing beans? I've thought about making a Qualifier so you can do something like

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

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

But that seemed clumsy, and would also require my @Producer to call new() - which I think would not allow the EntityManager to be injected into the DataModel. Also I'm not sure how you would require the qualifier to be added by the developer.

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

Or perhaps there's a better way to approach this, and I'm missing something?

推荐答案

我使用 seam-persistence 模块。 :

制作人:

public class EntityManagerProducer {

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

然后你可以@Inject实体经理。

Then you can @Inject the entity manager.

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

Otherwise, there is the DeltaSpike project that seems promising (never used it yet)

这篇关于用于数据模型的CDI生产者方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆