JSF 2 converters and Spring services
Updated: 2010-09-29
Problem
I have a JavaServer Faces 2 converter (using @FacesConverter
) and I need to access a Spring service. The converter is managed by JSF, and if I try to use @Service
or autowire a Spring bean, I get a null pointer exception.
Solution
Access the Spring service using FacesContextUtils
. For example:
@Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) {
// Your code here
if (personneService == null) {
personneService = (PersonneService) FacesContextUtils.getWebApplicationContext(facesContext)
.getBean("personneService");
}
// Rest of your implementation
}
This approach allows you to access Spring-managed beans from within a JSF converter.