Cacheable的key加上常量

    科技2025-02-08  16

    背景

      由于缓存的注解有类似的前缀,所以抽取出常量。

    public static final String KEY_CERT_TYPE_CODE_PREFIX = "ec_cert_type:cert_type_code:"; @Override @Cacheable(key = KEY_CERT_TYPE_CODE_PREFIX + "+#code") public EcCertType loadCertTypeByCode(String code) { // something return type; }

    但是使用时,报错:

    EL1041E: After parsing a valid expression, there is still more data in the expression

    经修改,给常量加上单引号,使:不被SpEL解析,解决。

    public static final String KEY_CERT_TYPE_CODE_PREFIX = "'ec_cert_type:cert_type_code:'"; @Override @Cacheable(key = KEY_CERT_TYPE_CODE_PREFIX + "+#code") public EcCertType loadCertTypeByCode(String code) { // something return type; }

    参考https://blog.csdn.net/chuixue24/article/details/103794706。

    Processed: 0.010, SQL: 8