Spring配置yml配置映射为HashMap

得意莫骄傲,失意莫沮丧。 / 2023-05-11 / 原文

核心代码

spring:
  cache:
    type: REDIS
    redis:
      time-to-live: 28800000
      custom-ttl: { "xxx" : "1000000", "yyy": "6000000"}

 

@Data
@Component
@Configuration
@ConfigurationProperties(prefix = "spring.cache.redis")
public class HiteamRedisConfig {

    //如果为空默认为12个小时
    @Value("${spring.cache.redis.time-to-live:43200000}")
    private Long redisKeyTtl;

    private HashMap<String, Long> customTtl;

 

注意,这里使用@Value无法注入,spring版本:2.2.6