使SpringBoot⾃定义注解⽀持EL表达式⾃定义注解
⾃定义DistributeExceptionHandler注解,该注解接收⼀个参数attachmentId。
该注解⽤在⽅法上,使⽤该注解作为切点,实现标注该注解的⽅法抛异常后的统⼀处理。
/**
* @Author: Lijian
* @Date: 2019-09-06 09:26
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DistributeExceptionHandler {
el表达式获取map的值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<String> 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) Signature()).getMethod(), Args());
AnnotatedElementKey methodKey = new AnnotatedElementKey(((MethodSignature) Signature()).getMethod(), Target().getCl ass());
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
/**
* @Author: Lijian
* @Date: 2019-09-06 09:26
*/
public class ExpressionEvaluator<T> extends CachedExpressionEvaluator {
private final ParameterNameDiscoverer paramNameDiscoverer = new DefaultParameterNameDiscoverer();
private final Map<ExpressionKey, Expression> conditionCache = new ConcurrentHashMap<>(64);
private final Map<AnnotatedElementKey, Method> 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<T> 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;
}
}
备注
本⽂参考:

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