Spring service is null in custom ConstraintValidator

375 Views Asked by At

I have spring boot project, a bean using javax validation API.

I'm trying to inject a service in my custom validator for a bean. But the service object is null.
I don't find a complete example that use Spring ConstraintValidator and i don't understand the configuration that some persons made for their integration tests in another topics.

I want to have a validator with constructor injection like this (i don't want to use @Autowired nore javax @Inject) :

@Component
public class CustomValidator implements ConstraintValidator<ValidCustomValidator, CustomObj> {

private ErreurService erreurService;

public CustomValidator(ErreurService erreurService) {
     this.erreurService=erreurService;
}

@Override
public boolean isValid(CustomObj customObj, ConstraintValidatorContext context) {
    erreurService.getMessages("");
    return false;
}
}

** ErreurService**

@Service("ErreurService")
@Transactional
public class ErreurServiceImpl implements ErreurService {//...}

With this approach i got an Exception HV000064: Unable to instantiate ConstraintValidator

I try something with @Autowired but got always got a NPE. Can you help me please ?

1

There are 1 best solutions below

1
Gordon from Blumberg On

Did you put the @Service annotation on ErreurService implementation class? Is it in the package from specified ComponentScan?