怎么把Java枚举名称作为注解的属性值

一、前言

  • Java注解的属性值,必须为 常量
  • 有些场景想把 枚举名称 设置为 注解的属性值(如 spring-cache 用枚举配置缓存,使用时 需要 缓存名称)

二、方案

方案一:名称属性 + 外部名称接口

1
2
3
4
5
6
7
8
9
@lombok.Getter
@lombok.AllArgsConstructor
public enum CommonCacheConfig {
    QUOTE_LEVEL(CommonCacheConstant.QUOTE_LEVEL, 2);

    private final String name;

    private final int ttl;
}
1
2
3
4
5
public interface CommonCacheConstant {

    String QUOTE_LEVEL = "QUOTE_LEVEL";

}

使用:@Cacheable(cacheNames = CommonCacheConstant.QUOTE_LEVEL)

方案二:名称属性 + 内部名称接口

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public enum CommonCacheConfig {
    QUOTE_LEVEL(Constant.QUOTE_LEVEL, 2);

    private final String name;
    private final Integer ttl;

    public interface Constant {
        String QUOTE_LEVEL = "QUOTE_LEVEL";
    }
}

使用:@Cacheable(cacheNames = CommonCacheConfig.Constant.QUOTE_LEVEL)

方案三:Lombok 的 FieldNameConstants

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@lombok.Getter
@lombok.AllArgsConstructor
@lombok.experimental.FieldNameConstants(onlyExplicitlyIncluded = true)
public enum CommonCacheConfig {

	@FieldNameConstants.Include QUOTE_LEVEL(2);

	private final Integer ttl;

}

编译以后的代码多了自动生成的 CommonCacheConfig$Fields.class:

1
2
3
4
5
6
7
public enum CommonCacheConfig implements ICacheConfig {

    public static final class Fields {
        public static final String QUOTE_LEVEL = "QUOTE_LEVEL";
    }

}

使用:@Cacheable(cacheNames = CommonCacheConfig.Fields.QUOTE_LEVEL)
注意:FieldNameConstants 的 onlyExplicitlyIncluded 需设置为 true,否则 按枚举的属性(如 ttl)生成,同时在 枚举项前加 @FieldNameConstants.Include

三、总结

本文首先发布于 https://www.890808.xyz/ ,其他平台需要审核更新慢一些。

javalover123