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
@FacesConverterisn’t an eligible injection target. Replace it by@ManagedBeanor@Named. As you’d like to use CDI as as well, use@Named.You only need to change the way it’s been referenced in the views. You cannot rely on
forClassanymore. You’d need to explicitly specify it as#{myConverter}.or
If you really need to keep the
@FacesConverterin favor offorClass, 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
@FacesConverteran eligible injection target in upcomingJSF 2.2, see also JSF spec issue 763JSF 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@FacesConverters (and@FacesValidators) in your webapp automatically become eligible for CDI and EJB injection without any extra effort.