如何注入@FacesConverter? [英] How can I inject in @FacesConverter?
问题描述
@FacesConverter(forClass = MyClass.class)
public MyConverter类实现转换器{
@EJB
private ClassForEJB classForEJB;
@Inject
private ClassForInject classForInject;
//转换方法
}
@FacesConverter
不是符合条件的注射目标。将其替换为 @ManagedBean
或 @Named
。如果您想使用CDI,请使用 @Named
。
@Named
@ApplicationScoped //我假设你的转换器没有任何状态。
public class MyConverter实现转换器{
// ...
}
您只需更改视图中引用的方式即可。你不能依赖 forClass
。您需要明确指定为#{myConverter}
。
< h:inputSomething ... converter =#{myConverter}/>
或
< h:inputSomething ...>
< f:转换器绑定=#{myConverter}/>
< / h:inputSomething>
如果您真的需要保留 @FacesConverter
赞成 forClass
,那么你需要手动抓取EJB的JNDI。 此博客文章中显示了一个具体示例。然而,我可以告诉CDI bean。
JSF的人已经确认了这个令人尴尬的监督,他们将使 @FacesConverter
即将到来的 JSF 2.2中的合格注入目标,另见 JSF规格问题763 JSF 2.3。
另请参见:
更新恰好使用JSF实用程序库 OmniFaces ,或者开放使用它,因为它的版本1.6,当在中有OmniFaces JAR时, / WEB-INF / lib
,全部 @FacesConverter
s(和 @FacesValidator
s)在你的webapp 自动无需任何额外的努力即可获得CDI和EJB注入资格。
I wrote a converter. I am using CDI and injection parallel. In that case the classes are not injected. How can I make the injection possible?
@FacesConverter(forClass = MyClass.class)
public class MyConverter implements Converter{
@EJB
private ClassForEJB classForEJB;
@Inject
private ClassForInject classForInject;
// Converter Methods
}
The @FacesConverter
isn't an eligible injection target. Replace it by @ManagedBean
or @Named
. As you'd like to use CDI as as well, use @Named
.
@Named
@ApplicationScoped // I assume that your converter doesn't have any state.
public class MyConverter implements Converter {
// ...
}
You only need to change the way it's been referenced in the views. You cannot rely on forClass
anymore. You'd need to explicitly specify it as #{myConverter}
.
<h:inputSomething ... converter="#{myConverter}" />
or
<h:inputSomething ...>
<f:converter binding="#{myConverter}" />
</h:inputSomething>
If you really need to keep the @FacesConverter
in favor of forClass
, then you'd need to grab the EJB manually by JNDI. A concrete example is shown in this blog article. I can however not tell that for CDI beans.
The JSF guys have confirmed this embarrassing oversight and they will make the @FacesConverter
an eligible injection target in upcoming JSF 2.2, see also JSF spec issue 763 JSF 2.3.
See also:
Update if you happen to use JSF utility library OmniFaces, or are open to using it, since its version 1.6, when just having OmniFaces JAR in /WEB-INF/lib
, all @FacesConverter
s (and @FacesValidator
s) in your webapp automatically become eligible for CDI and EJB injection without any extra effort.
这篇关于如何注入@FacesConverter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!