Search code examples
javaspring-bootcaffeine-cache

How to get map of settings from application.yml?


Here is my application.yml config:

spring:
  cache:
    cache-names: all_config_bundle, all_config_dependence, config_plan
    caffeine.spec: maximumSize=100, expireAfterAccess=0s
    caches:
      all_config_bundle: maximumSize=100, expireAfterAccess=0s
      all_config_dependence: maximumSize=100, expireAfterAccess=0s
      config_plan: maximumSize=100, expireAfterAccess=0s

I need to group them, that's why I add another part of settings. Here is my configuration class:

@Configuration
@EnableConfigurationProperties(CacheProperties.class)
@Setter
public class CacheConfig {

    private Map<String, String> caches;
}

CacheProperties already has a "spring.cache" prefix, so I only added a name of my group. I still get "null" in my map. What is my mistake?


Solution

  • With @ConfigurationProperties and prefix it works for me

    @ConfigurationProperties(prefix = "spring.cache")
    @Data
    @Configuration
    public class CacheConfig {
    
         private Map<String, String> caches;
    }
    

    Output :

    {all_config_bundle=maximumSize=100, expireAfterAccess=0s, all_config_dependence=maximumSize=100, expireAfterAccess=0s, config_plan=maximumSize=100, expireAfterAccess=0s}