package com.gxx.record.core; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import org.apache.log4j.Logger; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.reflect.MethodSignature; import com.gxx.record.utils.ValidationUtils; /** * 服务层AOP * @author Gxx */ public class ValidateAop { /** * 日志处理器 */ private final Logger logger = Logger.getLogger(ValidateAop.class); /** * 方法前后操作 * @param pjp * @return * @throws Exception */ public Object around(ProceedingJoinPoint pjp) throws Throwable { /** * 获取切面方法 */ Signature sig = pjp.getSignature(); MethodSignature msig = null; if (!(sig instanceof MethodSignature)) { throw new IllegalArgumentException("该注解只能用于方法"); } msig = (MethodSignature) sig; Object target = pjp.getTarget(); Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes()); /** * 判断是否加注解@LogMethodTime 和 是否落地 */ boolean isValidateAnnotation = currentMethod.isAnnotationPresent(Validate.class);//是否加注解@Validate /** * 判断是否加注解@Validate */ if(isValidateAnnotation) { Annotation p = currentMethod.getAnnotation(Validate.class); Method m = p.getClass().getDeclaredMethod("isValidate", null); boolean isValidate = (Boolean) m.invoke(p, null); /** * 是否需要校验 */ if(isValidate) { /** * 参数校验 */ Object[] args = pjp.getArgs(); if(null != args && args.length > 0) { for(Object arg : args) { ValidationUtils.validate(arg); } } } } /** * 执行方法 */ return pjp.proceed(); } }