注解解析SPEL表达式完成切面

1、创建ValidateTask切面

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target({ElementType.TYPE,ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ValidateTask {

    String expression() default "";

}

2、前置通知执行过程(前置通知、环绕通知)

import com.demo.qm.aop.parser.CachedExpParser;
import com.demo.aop.parser.ExpEvalContext;
import com.demo.aop.parser.MapperRouterInvocation;
import com.demo.validate.JMException;
import java.lang.reflect.Method;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Order(1)
@Component
@AllArgsConstructor
public class ValidateTaskAspect {

    @Pointcut("@annotation(com.lachesis.windranger.qm.aop.ValidateTask)")
    public void pointCut() {
    }


    @Before(value = "pointCut()")
    public void validateTask(JoinPoint joinPoint) {
        try {
            MapperRouterInvocation invocation = MapperRouterInvocation.newInstance(joinPoint);
            CachedExpParser cachedExpParser = CachedExpParser.newInstance(invocation);
            ExpEvalContext evalContext = cachedExpParser.createEvalContext(invocation);
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            Method method = methodSignature.getMethod();
            ValidateTask task = method.getAnnotation(ValidateTask.class);
            // String exp = "@taskValidateService.validatePlanTask(#p0,#p1,#p2)";
            // String exp = "@taskValidateService.validatePlanTask(#p0.taskCode,#p0.status,#p0.level)";
            // 从注解获取表达式,执行表达式
            cachedExpParser.parse(evalContext, task.expression());
        } catch (JMException jmException){
            throw jmException;
        }catch (Exception exception) {
            log.error("执行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值