元数据metadata中包含key生成器信息:
@Nullable
protected Object generateKey(@Nullable Object result) {
if (StringUtils.hasText(this.metadata.operation.getKey())) {
EvaluationContext evaluationContext = createEvaluationContext(result);
return evaluator.key(this.metadata.operation.getKey(), this.metadata.methodKey, evaluationContext);
}
return this.metadata.keyGenerator.generate(this.target, this.metadata.method, this.args);
}
再次跳转StepOver后进入到
自定义的KeyGenerator的@Bean方法:
package com.ezerbelcn.springboot.config;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Method;
import java.util.Arrays;
@Configuration
public class MyCacheConfig {
private StringBuilder sb;
private final String seperator = ":";
@Bean("myKeyGenerator")
public KeyGenerator keyGenerator(){
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
sb = new StringBuilder();
sb.append(method.getName());
sb.append(seperator);
sb.append(Arrays.asList(params).toString());
return sb.toString();
}
};
}
}