有时候我们使用枚举定义了很多常量,但又不想一个一个的去取这些常量,就可以使用反射的方式来遍历枚举常量。比如我们使用枚举来定义一个缓存的配置信息,这些配置信息会在缓存管理器初始化时使用到。
枚举类如下:
public enum CacheEnum {
/**产品ID缓存*/
PRODUCT_IDS("PRODUCT_IDS", "产品ID缓存" , 0, 0);
/**缓存名称*/
private String name;
/**缓存过期时间单位毫秒,0则永久缓存*/
private long ttl;
/**描述*/
private String text;
/**最大空闲时间,单位毫秒,多久未使用则丢弃,0则永不丢弃*/
private long maxIdleTime;
CacheEnum(String name, String text, long ttl, long maxIdleTime){
this.name = name;
this.ttl = ttl;
this.text = text;
this.maxIdleTime = maxIdleTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getTtl() {
return ttl;
}
public void setTtl(long ttl) {
this.ttl = ttl;
}
public long getMaxIdleTime() {
return maxIdleTime;
}
public void setMaxIdleTime(long maxIdleTime) {
this.maxIdleTime = maxIdleTime;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
使用反射遍历放到map里面,下面是测试代码:
public class CacheEnumTest {
@Test
public void test(){
Map<String, Map<String, Object>> config = new HashMap<String, Map<String, Object>>();
try {
Class<CacheEnum> clazz = CacheEnum.class;
Method name = clazz.getMethod("getName");
Method ttl = clazz.getMethod("getTtl");
Method maxIdleTime = clazz.getMethod("getMaxIdleTime");
for (Object obj : clazz.getEnumConstants()) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("ttl", ttl.invoke(obj));
map.put("maxIdleTime", maxIdleTime.invoke(obj));
config.put((String) name.invoke(obj), map);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println(JSON.toJSONString(config));
}
}
测试结果如下:
{"PRODUCT_IDS":{"maxIdleTime":0,"ttl":0}}
附上使用redisson缓存管理器(基于spring)的应用场景:
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.spring.cache.CacheConfig;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RedissonConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
@Value("${spring.redis.password}")
private String password;
/**
* RedissonClient,单机模式
*
* @return
* @throws IOException
*/
@Bean(destroyMethod = "shutdown")
public RedissonClient redisson() throws IOException {
Config config = new Config();
config.useSingleServer().setAddress("redis://" + host + ":" + port)
.setPassword(password);
RedissonClient redisson = Redisson.create(config);
return redisson;
}
@Bean
public CacheManager cacheManager(RedissonClient redissonClient) {
// 设置各个缓存的生命周期(TTL)
Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
try {
Class<CacheEnum> clazz = CacheEnum.class;
Method name = clazz.getMethod("getName");
Method ttl = clazz.getMethod("getTtl");
Method maxIdleTime = clazz.getMethod("getMaxIdleTime");
for (Object obj : clazz.getEnumConstants()) {
config.put((String) name.invoke(obj), new CacheConfig(
(Long) ttl.invoke(obj), (Long) maxIdleTime.invoke(obj)));
}
} catch (Exception e) {
throw new MembershipException(e);
}
RedissonSpringCacheManager manager = new RedissonSpringCacheManager(
redissonClient, config);
return manager;
}
}