自定义注解-利用spel表达式 获取方法入参对象的的 get方法参数

  @Anno(key = "#test.getName()")

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Anno {

    String key() ;
}


@Aspect
@Component
public class AnnoAspect {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private SpelExpressionParser parserSpel = new SpelExpressionParser();
    private DefaultParameterNameDiscoverer parameterNameDiscoverer= new DefaultParameterNameDiscoverer();
    
    @SuppressWarnings("rawtypes")
    @Around("@anno(ca)")
    public void cache(ProceedingJoinPoint pjp, anno ca) throws Exception {
        String value=  generateKeyBySpEL(ca.key(), pjp);
        try {
            pjp.proceed();
        } catch (Throwable throwable) {
            throw new Exception( "方法错误");
        } 
    }

    public String getValue(String key, ProceedingJoinPoint pjp) {
        Expression expression = parserSpel .parseExpression(spELString);
        EvaluationContext context = new StandardEvaluationContext();
      MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
       Object[] args = pjp.getArgs();
       String[] paramNames = parameterNameDiscoverer.getParameterNames(methodSignature.getMethod());
       for(int i = 0 ; i < args.length ; i++) {
            context.setVariable(paramNames[i], args[i]);
        }  
        return expression.getValue(context).toString();
    }

你可能感兴趣的:(自定义注解-利用spel表达式 获取方法入参对象的的 get方法参数)