SpringBoot⾃定义注解⽀持EL表达式(基于
MethodBasedEvaluatio。。。
⾃定义注解
⾃定义 DistributeExceptionHandler 注解,该注解接收⼀个参数 attachmentId 。
该注解⽤在⽅法上,使⽤该注解作为切点,实现标注该注解的⽅法抛异常后的统⼀处理。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DistributeExceptionHandler {
String attachmentId();
}
注解使⽤⽅法
@DistributeExceptionHandler(attachmentId = "#test.id")
public void test(Test test){
}
Aspect代码
拦截 DistributeExceptionHandler 注解作为切点
使⽤ @AfterThrowing 处理异常情况
@Component
@Aspect
@Slf4j
public class DistributeExceptionAspect {
@Autowired
private AttachmentContentClient attachmentContentClient;
@Autowired
private DistTaskService distTaskService;
private ExpressionEvaluator evaluator = new ExpressionEvaluator<>();
@Pointcut("@annotation(DistributeExceptionHandler)")
private void exceptionHandleMethod() {
}
@AfterThrowing(value = "exceptionHandleMethod()", throwing = "ex")
public void doThrowing(JoinPoint joinPoint, Throwable ex) {
<("捕获异常");
String attachmentId = getAttachmentId(joinPoint); // 获取
/
/ 处理异常情况下的业务
}
private DistributeExceptionHandler getDistributeExceptionHandler(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature) Signature();
Method method = Method();
Annotation(DistributeExceptionHandler.class);
}
private String getAttachmentId(JoinPoint joinPoint) {
DistributeExceptionHandler handler = getDistributeExceptionHandler(joinPoint);
if (Args() == null) {
return null;
}
EvaluationContext evaluationContext = Target(), Target().getClass(), ((MethodSignature) joinPoi        AnnotatedElementKey methodKey = new AnnotatedElementKey(((MethodSignature) Signature()).getMethod(), Target().getClass())        dition(handler.attachmentId(), methodKey, evaluationContext, String.class);
}
}
为注解添加Spring EL⽀持
ExpressionRootObject
public class ExpressionRootObject {
private final Object object;
private final Object[] args;
public ExpressionRootObject(Object object, Object[] args) {
this.object = object;
this.args = args;
}
public Object getObject() {
return object;
}
public Object[] getArgs() {
return args;
}
}
ExpressionEvaluator
public class ExpressionEvaluator extends CachedExpressionEvaluator {
private final ParameterNameDiscoverer paramNameDiscoverer = new DefaultParameterNameDiscoverer();
private final Map conditionCache = new ConcurrentHashMap<>(64);
private final Map targetMethodCache = new ConcurrentHashMap<>(64);
public EvaluationContext createEvaluationContext(Object object, Class targetClass, Method method, Object[] args) {
Method targetMethod = getTargetMethod(targetClass, method);
ExpressionRootObject root = new ExpressionRootObject(object, args);
return new MethodBasedEvaluationContext(root, targetMethod, args, this.paramNameDiscoverer);
}
public T condition(String conditionExpression, AnnotatedElementKey elementKey, EvaluationContext evalContext, Class clazz) {        return ditionCache, elementKey, conditionExpression).getValue(evalContext, clazz);
}
private Method getTargetMethod(Class targetClass, Method method) {
AnnotatedElementKey methodKey = new AnnotatedElementKey(method, targetClass);
Method targetMethod = (methodKey);
if (targetMethod == null) {
targetMethod = MostSpecificMethod(method, targetClass);
if (targetMethod == null) {
targetMethod = method;
}
this.targetMethodCache.put(methodKey, targetMethod);
}
return targetMethod;
}
}
参考:stackoverflow。
SPEL表达式计算引擎
import org.springframework.beans.factory.BeanFactory;
import t.expression.AnnotatedElementKey;
import t.expression.BeanFactoryResolver;
import t.expression.CachedExpressionEvaluator;
import t.expression.CachedExpressionEvaluator;
import t.expression.MethodBasedEvaluationContext;
import pression.EvaluationContext;
import pression.Expression;
import flect.Method;
import java.util.Map;
import urrent.ConcurrentHashMap;
public class SimpleExpressionEvaluator extends CachedExpressionEvaluator {
private final Map<ExpressionKey, Expression> conditionCache = new ConcurrentHashMap<ExpressionKey, Expression>(64);
public EvaluationContext createMethodBasedEvaluationContext(Method method, Object[] args, BeanFactory beanFactory) {        MethodBasedEvaluationContext evaluationContext =
new MethodBasedEvaluationContext(null, method, args, getParameterNameDiscoverer());
if (beanFactory != null) {
evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
}
return evaluationContext;
}
public <T> T eval(String expression, AnnotatedElementKey elementKey, EvaluationContext evalContext) {
return (T) getExpression(elementKey, expression).getValue(evalContext);
}
public Expression getExpression(AnnotatedElementKey elementKey, String expression) {
return ditionCache, elementKey, expression);
}
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package t.expression;
import java.util.Map;
import DefaultParameterNameDiscoverer;
import ParameterNameDiscoverer;
import pression.Expression;
import pression.spel.standard.SpelExpressionParser;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
public abstract class CachedExpressionEvaluator {
private final SpelExpressionParser parser;
private final ParameterNameDiscoverer parameterNameDiscoverer;
protected CachedExpressionEvaluator(SpelExpressionParser parser) {
this.parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
this.parser = parser;
}
protected CachedExpressionEvaluator() {
this(new SpelExpressionParser());
}
protected SpelExpressionParser getParser() {
return this.parser;
}
}
protected ParameterNameDiscoverer getParameterNameDiscoverer() {
return this.parameterNameDiscoverer;
}
protected Expression getExpression(Map<CachedExpressionEvaluator.ExpressionKey, Expression>
cache, AnnotatedElementKey elementKey, String express        CachedExpressionEvaluator.ExpressionKey expressionKey = ateKey(elementKey, expression);
Expression expr = ((expressionKey);
if (expr == null) {
expr = Parser().parseExpression(expression);
cache.put(expressionKey, expr);
}
return expr;
}
private CachedExpressionEvaluator.ExpressionKey createKey(AnnotatedElementKey elementKey, String expression) {
return new CachedExpressionEvaluator.ExpressionKey(elementKey, expression);
}
protected static class ExpressionKey implements Comparable<CachedExpressionEvaluator.ExpressionKey> {
private final AnnotatedElementKey element;
private final String expression;
protected ExpressionKey(AnnotatedElementKey element, String expression) {
this.element = element;
}
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (!(other instanceof CachedExpressionEvaluator.ExpressionKey)) {
return false;
} else {
CachedExpressionEvaluator.ExpressionKey otherKey = (CachedExpressionEvaluator.ExpressionKey)other;
return this.element.equals(otherKey.element) && ObjectUtils.pression, pression);
}
}
public int hashCode() {
return this.element.hashCode() + (pression != null ? pression.hashCode() * 29 : 0);
el表达式获取map的值
}
public String toString() {
return this.element + (pression != null ? " with expression \"" + pression : "\"");
}
public int compareTo(CachedExpressionEvaluator.ExpressionKey other) {
int result = String()pareTo(String());
if (result == 0 && pression != null) {
result = pression);
}
return result;
}
}
}
核⼼接⼝
EvaluationContext

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。