fluent-validator
fluent-validator copied to clipboard
和HibernateValidator集成使用时 每次都需要 setHibernateValidator 太过麻烦
Result ret = FluentValidator.checkAll()
.on(company, new HibernateSupportedValidator<Company>().setHibernateValidator(validator))
.on(company, new CompanyCustomValidator())
.doValidate().result(toSimple());
另外
public class HibernateSupportedValidator<T> extends ValidatorHandler<T> implements Validator<T> {
private static javax.validation.Validator HIBERANTE_VALIDATOR;
private int hibernateDefaultErrorCode;
private ConstraintViolationTransformer constraintViolationTransformer = new DefaultConstraintViolationTransformer();
HIBERANTE_VALIDATOR 为static 何解?
-
Because Fluent-validator is not bounded to hibernate validator, so it is intend to be created manually. If you think setHibernateValidator is redundant, I suggest using
Factorydesign pattern to build it, note thatHibernateSupportedValidatoris not thread-safe, must be prototyped. -
Validator instance is retrieved from the ValidatorFactory. Validator instances are thread-safe and may be reused multiple times. This is how we use Hibernate in the right way.