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("执行