分类: Java
2017-01-29 20:54:24
新建项目, 在pom文件中添加如下maven依赖
javax.validation validation-api 1.1.0.Final org.hibernate hibernate-validator 5.2.1.Final org.hibernate hibernate-validator-annotation-processor 5.2.1.Final javax.el javax.el-api 2.2.4
如下是使用validation验证bean的使用示例:
package cn.outofmemory.hello; import org.hibernate.validator.constraints.Email; import javax.validation.ConstraintViolation; import javax.validation.Valid; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Set; /** * Created by outofmemory.cn on 2016/6/28. */ public class HelloValidator { public static class Student { private int id; @NotNull @Size(max = 20, min=2, message = "student name length must between 2 to 20") private String name; @NotNull @Email private String email; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } public static void main(String[] args) { Student student = new Student(); student.setName("a"); student.setEmail("ah"); // // ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); // Validator validator = validatorFactory.getValidator(); // Set> violations = validator.validate(student); // for (ConstraintViolationviolation : violations) { // System.out.println(violation.getMessage()); // } AService aService = (AService) Proxy.newProxyInstance( HelloValidator.class.getClassLoader(), new Class[]{AService.class}, new ValidationInvocationHandler(new AServiceImpl()) ); aService.processStudent(student); } public interface AService { void processStudent(@Valid Student student); } public static class AServiceImpl implements AService{ public void processStudent(Student student) { System.out.println("do something"); } } public static class ValidationInvocationHandler implements InvocationHandler { static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); final Object proxyTarget; public ValidationInvocationHandler(final Object target) { proxyTarget = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Annotation[][] annotations = method.getParameterAnnotations(); for (int parameterIndex = 0; parameterIndex < args.length; parameterIndex++) { Annotation[] parameterAnnotations = annotations[parameterIndex]; for(Annotation parameterAnnotation : parameterAnnotations) { if (parameterAnnotation.annotationType().isAssignableFrom(Valid.class)) { //we need validate this parameter Object parameter = args[parameterIndex]; Set<ConstraintViolation<Object>> violations = validator.validate(parameter); if (violations.size() > 0) { for (ConstraintViolation<Object> violation : violations) { System.out.println(violation.getMessage()); } } } } } return method.invoke(proxyTarget, args); } } }