spring之自定义注解
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy =MyConstraintValidator.class )
public @interface MyAnnotation {
String message() default "王某自定义注解";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
@Slf4j
public class MyConstraintValidator implements ConstraintValidator<MyAnnotation,Object> {
private final String MOBILE_REGEXP = "^[1][3,4,5,6,7,8,9][0-9]{9}$";
@Override
public void initialize(MyAnnotation constraintAnnotation) {
log.info("初始化失败!");
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
// if(flag){
// //TODO 做自己的验证 业务操作
// //TRUE标识返回成功
// }
String strValue = String.valueOf(value);
if (ObjectUtils.isEmpty(value)) {
return Pattern.matches(MOBILE_REGEXP, strValue);
}
return false;
}
}
public class AdminUser {
private String name;
private String password;
@MyAnnotation
private String phone;
}
注解介绍请看;https://www.cnblogs.com/wangbiaohistory/p/12522153.html
本文来自博客园,作者:余生请多指教ANT,转载请注明原文链接:https://www.cnblogs.com/wangbiaohistory/p/16913063.html

浙公网安备 33010602011771号