Pass method argument in Aspect of custom annotation

前端 未结 5 2002
说谎
说谎 2021-02-13 03:14

I\'m trying to use something similar to org.springframework.cache.annotation.Cacheable :

Custom annotation:

@Target(ElementType.METHOD)
             


        
5条回答
  •  南方客
    南方客 (楼主)
    2021-02-13 03:58

    Spring uses internally an ExpressionEvaluator to evaluate the Spring Expression Language in the key parameter (see CacheAspectSupport)

    If you want to emulate the same behaviour, have a look at how CacheAspectSupport is doing it. Here is an snippet of the code:

    private final ExpressionEvaluator evaluator = new ExpressionEvaluator();
    
        /**
         * Compute the key for the given caching operation.
         * @return the generated key, or {@code null} if none can be generated
         */
        protected Object generateKey(Object result) {
            if (StringUtils.hasText(this.metadata.operation.getKey())) {
                EvaluationContext evaluationContext = createEvaluationContext(result);
                return evaluator.key(this.metadata.operation.getKey(), this.methodCacheKey, evaluationContext);
            }
            return this.metadata.keyGenerator.generate(this.target, this.metadata.method, this.args);
        }
    
        private EvaluationContext createEvaluationContext(Object result) {
            return evaluator.createEvaluationContext(
                    this.caches, this.metadata.method, this.args, this.target, this.metadata.targetClass, result);
        }
    

    I don't know which IDE you are using, but it must deal with the @Cacheable annotation in a different way than with the others in order to highlight the params.

提交回复
热议问题