In one of my Spring applications, I am planning to use MethodValidationPostProcessor
for validating service method parameters. As I know, for this, I'll have to have a MethodValidationPostProcessor
object in the ApplicationContext, which in turn will have to have one validator
injected. I am doing it like this in a @Configuration
class:
@Bean
public LocalValidatorFactoryBean validator() {
return new LocalValidatorFactoryBean();
}
@Bean
public MethodValidationPostProcessor getMethodValidationPostProcessor(){
MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
processor.setValidator(validator());
return processor;
}
And then, my service classes look like this:
@Service
@Validated
public class SomeService {
public void someMethod(@Valid SomeForm someForm) {
.
.
.
}
}
But, a few of my objects would be needing some custom validators. For example, for the signup form, I plan to use a JoinDataValidator extending the LocalValidatorFactoryBean
, like this:
@Service("joinDataValidator")
public class JoinDataValidator extends LocalValidatorFactoryBean {
@Override
public void validate(final Object target, final Errors errors) {
super.validate(target, errors);
// check for duplicate email
}
@Override
public boolean supports(Class<?> clazz) {
return JoinForm.class.equals(clazz);
}
}
So, I'll be needing multiple validators, for different service classes. I could not figure out how to tell the configured MethodValidationPostProcessor
to use different validators depending on the Service class being validated, or what could be a good way to handle this kind of scenario. I would appreciate help.
Update: Have created a ticket: https://jira.spring.io/browse/SPR-12563