springboot中通过AOP+SpEL获取接口参数对象的属性值

目录:
1、简单介绍SpEL用法
2、写注解
3、写AOP
4、通过SpEL获取接口参数
5、使用
正文:
1、简单介绍SpEL
Spring Expression Language(缩写为SpEL)是一种强大的表达式语言。在Spring产品组合中,它是表达式计算的基础。它支持在运行时查询和操作对象图,它可以与基于XML和基于注解的Spring配置还有bean定义一起使用。由于它能够在运行时动态分配值,因此可以为我们节省大量Java代码。
SpEL官方文档

2、写注解

@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CleanerTimeCheck {
    String cleanerCode() default "";
    String orderTime() ;
}

3、写AOP
4、通过SpEL获取接口参数

@Aspect
@Component
@Order(2)
@Log4j2
public class CleanerTimeCheckAop {

    @Autowired
    private IOrOrderService orOrderService;

    private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Pointcut("@annotation(cleanerTimeCheck)")
    public void initLogAnno(CleanerTimeCheck cleanerTimeCheck) {
    }

    @Before("initLogAnno(cleanerTimeCheck)")
    public void doBefore(JoinPoint point, CleanerTimeCheck cleanerTimeCheck) {
		//通过SpEL获取接口参数对象属性值
        StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext(point.getArgs());
        standardEvaluationContext = setContextVariables(standardEvaluationContext, point);
        String cleanerCode = getElValue(cleanerTimeCheck.cleanerCode(),standardEvaluationContext);
        String orderTime = getElValue(cleanerTimeCheck.orderTime(),standardEvaluationContext);

       业务代码,省略...


    }

    private StandardEvaluationContext setContextVariables(StandardEvaluationContext standardEvaluationContext,
                                                          JoinPoint joinPoint) {

        Object[] args = joinPoint.getArgs();
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method targetMethod = methodSignature.getMethod();
        LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer
                = new LocalVariableTableParameterNameDiscoverer();
        String[] parametersName = parameterNameDiscoverer.getParameterNames(targetMethod);

        if (args == null || args.length <= 0) {
            return standardEvaluationContext;
        }
        for (int i = 0; i < args.length; i++) {
            standardEvaluationContext.setVariable(parametersName[i], args[i]);
        }
        return standardEvaluationContext;
    }

    /**
     * 通过key SEL表达式获取值
     *
     */
    private String getElValue(String key, StandardEvaluationContext context) {
        if (StringUtils.isBlank(key)) {
            return "";
        }
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(key);
        String value = exp.getValue(context, String.class);

        return value;

    }
}

5、使用

 @CleanerTimeCheck(cleanerCode = "#payPrepayVO.cleanerCode",orderTime = "#payPrepayVO.serviceTime")
    public Result prepay(@RequestBody @Validated OrderPrepayVO payPrepayVO, @LoginUser String customCode) throws InterruptedException {
    业务代码,省略...
}

你可能感兴趣的:(java,java,spring)