Spring @Autowired into JSF custom validator

Here’s the scenario, create a custom JSF validator, injects a bean via Spring’s @Autowired.

UsernameValidator.java – Custom JSF validator
package com.mkyong.user;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.mkyong.user.bo.UserService;
public class UsernameValidator implements Validator {
	UserService userService;
	public void validate(FacesContext context, UIComponent component,
		Object value) throws ValidatorException {
	  String username = value.toString();
		FacesMessage facesMsg = new FacesMessage("Username is duplicated");
		throw new ValidatorException(facesMsg);

In JSF / XHTML page, link it via validatorId.

<h:inputText id="username">
	<f:validator validatorId="usernameValidator" />


During application startup, log file shows that Spring is created the “UsernameValidator” bean, and UserService is injected successful.

However, when access from JSF page, the bean userService from “UsernameValidator” is showing null, caused a NullPointerException.


This is because @FacesValidator isn’t managed by Spring’s container. To fix it, reference the custom validator via binding, instead of validatorId.

P.S Spring, CDI and JSF, did a really bad job here (integrate each others), hope they can fix it in future release.

<h:inputText id="username">
	<f:validator binding="#{usernameValidator}" />

In custom validator class, uses Spring or CDI only, @FacesValidator is not necessary.

UsernameValidator.java – Spring Only
public class UsernameValidator implements Validator {
	UserService userService;
UsernameValidator.java – CDI Only
public class UsernameValidator implements Validator {
	UserService userService;

Of course, make sure Spring component auto scan is enabled.

<beans xmlns="http://www.springframework.org/schema/beans"
	<context:component-scan base-package="com.mkyong" />
	<context:annotation-config />


  1. Named object not found
  2. JSF 2 custom validator example
Tags :

About the Author

Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.


  • Jesh

    binding=”dcValidator”: Cannot convert dcValidator of type class java.lang.String to interface javax.faces.validator.Validator

    please help.

    • Jesh

      Sorry little-bit rush, forget EL Expression in binding. But still i am getting NullPointerException.

  • klimpond

    Just one memo to add. Do not throw ValidatorException(FacesMessage) in your validate(…) method. These exceptions will propagete to your code.

    Try using FacesContext instead:

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {

    context.addMessage(component.getClientId(context), duplicateMsg);


  • klimpond

    Thanks very much. Just what I was looking for. Keep bloging :)

  • Fadhel

    First, i would like to thank you for your big work, your tutorial is very helpful.
    I tried to follow your tutorial for spring inject with validator using binding instead of validatorId. I noticed, if you have more than one validation you have to use @Componenet(“uniqueName”), and you call . Otherwise, the spring framework will not do the dependecy injection correctly since all validator classes will Heritage from javax.faces.validator.Validator.
    It’s like the dependency inject is done by type, if you don’t use @Componenet(“uniqueName”).


  • Pingback: Custom validator in JSF 2.0()