Contents
1. Background
- Java annotation value must be constant
- But some case want to use enum name(eg. spring-cache use enum config cache, operate cache need cache name)
2. Plan
Plan 1: name property + external name interface
|
|
|
|
Usage:@Cacheable(cacheNames = CommonCacheConstant.QUOTE_LEVEL)
Plan 2: name property + internal name interface
|
|
Usage:@Cacheable(cacheNames = CommonCacheConfig.Constant.QUOTE_LEVEL)
Plan 3:Lombok’s FieldNameConstants
|
|
Usage:@Cacheable(cacheNames = CommonCacheConfig.Fields.QUOTE_LEVEL)
Attention: FieldNameConstants’s onlyExplicitlyIncluded must set to true,otherwise generate by property(eg. ttl), and add @FieldNameConstants.Include before enum item.
3. Summary
- Use Lombok’s FieldNameConstants auto generate enum name constant, improved maintainability
- Refer:java - Use Enum type as a value parameter for @RolesAllowed-Annotation - Stack Overflow
The post first published at https://www.890808.xyz/ ,others update slowly.