I have a FacesValidator
class named UniqueEmailValidator
to determine that the entered email is unique or not.
I need the userService
variable, so i inject it by spring.
Here is the UniqueEmailValidator.java
:
@FacesValidator("com.obs.bean.uniqueEmailValidator")
@Component
public class UniqueEmailValidator implements Validator {
@Autowired
private UserService userService;
public void validate(FacesContext fc, UIComponent uic, Object value) throws ValidatorException {
if (value == null) {
return;
}
String email = (String) value;
System.out.println("userService before use: " + userService); // null
boolean exists = userService.emailExists(email);
if (exists) {
throw new ValidatorException(new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Email already in use", null));
}
}
}
Here is the UserServiceImple.java
:
@Service
public class UserServiceImpl implements UserService<User, Integer>, Serializable {
@Autowired
@Qualifier("userDao")
private UserDao userDao;
@Transactional()
public boolean emailExists(String value) {
Query query = userDao.getSession().createQuery("from User where email=?");
query.setString(0, value);
if (query.uniqueResult() != null) {
return true;
}
return false;
}
}
But the userService
is null
, (I get nullPointerException
)
I have also This lines in applicationContext.xml
:
<!-- Activates scanning of @Autowired -->
<context:annotation-config/>
<!-- Activates scanning of @Repository and @Service -->
<context:component-scan base-package="..."/>