How can I inject in @FacesConverter?

Kayser picture Kayser · Oct 31, 2012 · Viewed 10.6k times · Source

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
}

Answer

BalusC picture BalusC · Oct 31, 2012

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 @FacesConverters (and @FacesValidators) in your webapp automatically become eligible for CDI and EJB injection without any extra effort.