Spring AOP获取方法的参数名称和参数值

时间:2021-06-26 08:08:50

aop配置: <aop:aspectj-autoproxy expose-proxy="true" />

@Before(value = "execution(* com..xxxxx.web..*.*(..))")
public void methodBefore(JoinPoint joinPoint) {
try {
//类名
String clazzName = joinPoint.getTarget().getClass().getName();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//方法名
String methodName = methodSignature.getName();
//参数名数组
String[] parameters = methodSignature.getParameterNames();
//参数值
Object[] args = joinPoint.getArgs(); //获取参数名对应数组下标
int paramIndex = ArrayUtils.indexOf(parameters,"page");
Page<T> page = null ;
if (paramIndex != -1){
//参数值与参数名下标一致,获取分页page类
page =( Page<T>) args[paramIndex];
}else{
return ;
} //请求原始值
int pageSize = page.getPageSize();
//配置最大值
int maxPageSize = config.getMaxPageSize();
//配置默认值
int defaultPageSize = config.getDefaultPageSize();
//请求值超过安全阈值,拦截处理
if (pageSize > maxPageSize) {
logger.warn("==pageSize参数异常=="+clazzName+"-"+methodName+":"+"pageSize大于配置最大值:"+maxPageSize+"!将设置默认值:"+defaultPageSize);
page.setPageSize(defaultPageSize);
}
}catch (Exception ex){
logger.error("分页配置发生异常!",ex);
}
}