JSF 2.3 Using a CDI managed Converter

Although a converter is considered an attached object (part of the view state) commonly people ask support for CDI injection into a converter. Since we did not want to upset the original contract of what a converter is we had to look at how we can best support the requested use case.

We already had an annotation (@FacesConverter) that was being used to define a converter programmatically. To facilitate the use case we decided to add an additional attribute (managed) that states whether or not the Converter in question needs to be managed by CDI.

So how does that look in code?


    @FacesConverter(value = "myConverter", managed=true)
    public class MyConverter implements Converter {
    }
        

Note in this example the Converter would be a @RequestScoped bean as we did not state what CDI scope needs to manage it. You can use any other scope available to CDI. Beneath the covers we do not save the actual Converter instance into the view state, but we save the value "myConverter" so upon state restore we can ask CDI for the instance. And voila Converters can now be CDI managed!

Posted January 15, 2015

Up