Created
March 22, 2012 03:06
-
-
Save scottfrederick/2155354 to your computer and use it in GitHub Desktop.
Spring Meta-validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- Register the meta-validator bean to MVC --> | |
| <mvc:annotation-driven validator="validator"/> | |
| <!-- Enable component scanning on the package(s) containing annotated validators --> | |
| <context:component-scan base-package="org.example.domain.validators"/> | |
| <!-- Declare the meta-validator bean --> | |
| <bean id="validator" class="org.example.validation.annotation.ValidatorComponentAnnotationValidator"> | |
| <property name="validators"> | |
| <util:list> | |
| <!-- Enable JSR-303 bean validation --> | |
| <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> | |
| </util:list> | |
| </property> | |
| </bean> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @ValidatorComponent | |
| public class PersonValidator implements Validator { | |
| /** | |
| * This Validator validates just Person instances | |
| */ | |
| public boolean supports(Class clazz) { | |
| return Person.class.equals(clazz); | |
| } | |
| public void validate(Object obj, Errors e) { | |
| ValidationUtils.rejectIfEmpty(e, "name", "name.empty"); | |
| Person p = (Person) obj; | |
| if (p.getAge() < 0) { | |
| e.rejectValue("age", "negativevalue"); | |
| } else if (p.getAge() > 110) { | |
| e.rejectValue("age", "too.darn.old"); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Target({ElementType.TYPE}) | |
| @Retention(RetentionPolicy.RUNTIME) | |
| @Component | |
| public @interface ValidatorComponent { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ValidatorComponentAnnotationValidator implements Validator, InitializingBean { | |
| @Autowired | |
| private List<validator> validatorBeans; | |
| private Collection<validator> validators = new ArrayList<validator>(); | |
| public void afterPropertiesSet() throws Exception { | |
| findAnnotatedValidatorBeans(); | |
| } | |
| public boolean supports(Class clazz) { | |
| for (Validator validator : validators) { | |
| if (validator.supports(clazz)) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| public void validate(Object target, Errors errors) { | |
| for (Validator validator : validators) { | |
| if (validator.supports(target.getClass())) { | |
| validator.validate(target, errors); | |
| } | |
| } | |
| } | |
| private void findAnnotatedValidatorBeans() { | |
| for (Validator bean : validatorBeans) { | |
| ValidatorComponent annotation = AnnotationUtils.findAnnotation(bean.getClass(), ValidatorComponent.class); | |
| if (annotation != null) { | |
| validators.add(bean); | |
| } | |
| } | |
| } | |
| public void setValidators(Collection<validator> validators) { | |
| this.validators = validators; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment