数据模型的CDI生产者方法
我希望能够 @Inject 支持 RichFaces 4 ExtendedDataTable 的数据模型,但它需要 EntityManager 来完成其工作。当然,EntityManager 的查询需要知道类,我不想将其传递到方法调用中(在这种情况下,我的代码不会调用这些方法);理想情况下它应该在构造函数中。
像这样的事情:
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);
}
是否可以创建一个可用于将此 DataModel 注入到我的支持 bean 中的 CDI @Producer?我考虑过制作一个限定符,这样你就可以做类似的事情
@Inject @JType(value = MyEntity.class) DataModel<MyEntity> dataModel;
但这看起来很笨拙,并且还需要我的 @Producer 调用 new()
- 我认为这不允许注入 EntityManager进入数据模型。另外,我不确定您如何要求开发人员添加限定符。
或者也许有更好的方法来解决这个问题,而我错过了一些东西?
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.
Something like this:
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);
}
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;
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用 seam-persistence< /a>来自seam3的模块。 :
Producer :
然后你可以@Inject实体管理器。
否则,有一个看起来很有前途的 DeltaSpike 项目(从未使用过)还没有)
I do this using the seam-persistence module from seam3. :
Producer :
Then you can @Inject the entity manager.
Otherwise, there is the DeltaSpike project that seems promising (never used it yet)