How can I inject a dependency like @EJB, @PersistenceContext, @Inject, @AutoWired, etc in a @FacesConverter? In my specific case I need to inject an EJB via @EJB:
@FacesConverter
public class MyConverter implements Converter {
@EJB
protected MyService myService;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
// myService.doSomething
}
}
However, it didn’t get injected and it remains null, resulting in NPEs. It seems that @PersistenceContext and @Inject also doesn’t work.
How do I inject a service dependency in my converter so that I can access the DB?
No, not until JSF 2.3 is released. The JSF/CDI guys are working on that for JSF 2.3. See also JSF spec issue 1349 and this related “What’s new in JSF 2.3?” article of my fellow Arjan Tijms. Only then dependency injection like
@EJB,@PersistenceContext,@Inject, etc will work in a@FacesConverterwhen you explicitly addmanaged=trueattribute to the annotation.Before JSF 2.3, you have several options:
Make it a managed bean instead. You can make it a JSF, CDI or Spring managed bean via
@ManagedBean,@Namedor@Component. The below example makes it a JSF managed bean.And the below example makes it a CDI managed bean.
Reference it as
<h:inputXxx converter="#{yourConverter}">instead of<h:inputXxx converter="yourConverter">, or as<f:converter binding="#{yourConverter}">instead of<f:converter converterId="yourConverter">. Don’t forget to remove the@FacesConverterannotation!The disadvantage is that you cannot specify
forClassand thus need to manually define the converter everywhere in the view where necessary.Inject it in a regular managed bean instead.
And in your converter, grab or call it via EL.
This way you can keep using
@FacesConverter.Manually grab the EJB from JNDI.
The disadvantage is that there is a certain risk that this is not entirely portable. See also Inject EJB bean from JSF managed bean programmatically.
Install OmniFaces. Since version 1.6, it transparently adds support for
@EJB(and@Inject) in a@FacesConverterwithout any further modification. See also the showcase. If you happen to need the converter for<f:selectItem(s)>, then the alternative is to use itsSelectItemsConverterwhich will automatically perform the conversion job based on select items without the need for any database interaction.See also Conversion Error setting value for 'null Converter'.
See also:
@EJBin a@FacesValidatorand@FacesConverter