com.healthy.common.log.operation.resolver.impl.ExpressionOperationLoggingKeyResolver Maven / Gradle / Ivy
package com.healthy.common.log.operation.resolver.impl;
import cn.hutool.core.util.ArrayUtil;
import com.healthy.common.log.operation.annotation.OperationLogging;
import com.healthy.common.log.operation.resolver.OperationLoggingKeyResolver;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import java.lang.reflect.Method;
/**
* 基于 Spring EL 表达式,
*
* @author xm.z
*/
public class ExpressionOperationLoggingKeyResolver implements OperationLoggingKeyResolver {
private final ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
private final ExpressionParser expressionParser = new SpelExpressionParser();
private static Method getMethod(JoinPoint point) {
MethodSignature signature = (MethodSignature) point.getSignature();
return signature.getMethod();
}
@Override
public String resolver(JoinPoint joinPoint, OperationLogging operationLogging) {
// 获得被拦截方法参数名列表
Method method = getMethod(joinPoint);
Object[] args = joinPoint.getArgs();
String[] parameterNames = this.parameterNameDiscoverer.getParameterNames(method);
// 准备 Spring EL 表达式解析的上下文
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
if (ArrayUtil.isNotEmpty(parameterNames)) {
for (int i = 0; i < parameterNames.length; i++) {
evaluationContext.setVariable(parameterNames[i], args[i]);
}
}
// 解析参数
Expression expression = this.expressionParser.parseExpression(operationLogging.expression());
return expression.getValue(evaluationContext, String.class);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy