先随便定义一个自定义注解
@Target({
ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LimitLength {
int minLength() default 1;
int maxLength() default 1;
}
这里预计用来限制字段的长度,本来最开始的设想是限制密码长度的(只是随便实现个功能,不要纠结加密、DTO的事),所以在加到方法上时(即下面aop处理时)仅仅处理了密码的长度。
@Aspect
@Component
public class LimitLengthAspect {
@Before("@annotation(com.islands.common.aspect.annotation.LimitLength)")
public void checkBefore(JoinPoint point) {
// 获取注解
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
LimitLength annotation = method.getDeclaredAnnotation(LimitLength.class);
// 获取实体
Object[] args = point.getArgs();
for (Object arg : args) {
if (arg instanceof User) {
String password = ((User</