将资源注入 UIComponent(又名 CDI 在这里工作吗?)
我正在编写一个需要与我的 DAO 交互的(复合)组件。以下是 Java 部分的声明方式:
@FacesComponent(value="selectLocation")
public class SelectLocation extends UINamingContainer {
为了获取 DAO 对象,我尝试了 CDI 注释:
@Inject private LocationControl lc;
但这不起作用,所以我尝试了 Faces 注释:
@ManagedProperty (value = "@{locationControl}") private LocationControl lc;
两种情况都没有发生 - 属性 lc 在构造函数之后最终为 null完成。
我在所有支持 bean 中使用 CDI,一切正常。这将在 GlassFish 3.1.1 中使用 Weld。关于如何获取资源有什么建议吗?
I am writing a (composite) component that needs to interact with my DAO. Here is how the Java part is declared:
@FacesComponent(value="selectLocation")
public class SelectLocation extends UINamingContainer {
To get the DAO object, I tried the CDI annotation:
@Inject private LocationControl lc;
And that didn't work so I tried the Faces annotation:
@ManagedProperty (value = "@{locationControl}") private LocationControl lc;
Both cases nothing happens -- the property lc ends up as null after the constructor finishes.
I use CDI in all my backing beans and it all works. This would be using Weld inside GlassFish 3.1.1. Any suggestions on how to get the resource?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我现在有一个解决方法,基本上就是放入 CDI 等的样板代码中。等人。应该废除。我现在有这个方法:
我想知道是否有人有更好的解决方案。
I have a work-around for now, which is to basically put in the boiler-plate code that CDI et. al. is supposed to do away with. I now have this method:
I would like to know if anyone has a better solution.
是的,只需按照通常的方式使用辅助 bean 即可。
如有必要,将其包装在可重复使用的标记文件/复合材料中以保持干燥:
Yes, just use a backing bean the usual way.
Wrap if necessary in a reusable tagfile/composite to keep it DRY:
我不知道它是否也适用于组件,但是使用 CDI + MyFaces CODI,您可以使用 @Advanced 来标记例如应该能够使用 @Inject 的 Phase-Listeners。如果它不起作用,您可以在他们的 JIRA 中创建功能请求。它们速度非常快并且发布频繁。
或者你使用:
MyBean myBean = BeanManagerProvider.getInstance().getContextualReference(MyBean.class);
手动。
I don't know if it also works for components, but with CDI + MyFaces CODI you have @Advanced to mark e.g. Phase-Listeners which should be able to use @Inject. If it doesn't work, you could create a feature request in their JIRA. They are pretty fast and there are frequent releases.
Or you use:
MyBean myBean = BeanManagerProvider.getInstance().getContextualReference(MyBean.class);
manually.