Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load contextual config in spring boot

I have a spring boot app which uses country wise config files. The structure of the config files is same but values are different for different countries.

I have created a directory for each country in resources directory and placed that country's config file there.

Based on the country code passed in request parameters, I want to use the corresponding config file. What's the spring boot idiomatic way to achieve this (Apart from manually loading the yaml config files using something like snakeyaml)?

Thanks

like image 407
Vikk Avatar asked Mar 07 '26 04:03

Vikk


1 Answers

You can acheive by creating bean for MessageSource,LocaleResolver and LocaleChangeInterceptor bean and then add LocaleChangeInterceptor to InterceptorRegistry like this:

@Configuration
    public class CountryConfig extends WebMvcConfigurerAdapter{

        @Bean
        public MessageSource messageSource() {
            ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
            messageSource.setBasenames("classpath:country-conf/country");
            messageSource.setUseCodeAsDefaultMessage(true);
            messageSource.setDefaultEncoding("UTF-8");
            messageSource.setCacheSeconds(3600);
            return messageSource;
        }

        @Bean
        public LocaleResolver localeResolver() {
            SessionLocaleResolver slr = new SessionLocaleResolver();
            slr.setDefaultLocale(Locale.US);
            return slr;
        }

        @Bean
        public LocaleChangeInterceptor localeChangeInterceptor() {
            LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
            lci.setParamName("country");
            return lci;
        }

        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(localeChangeInterceptor());
        }
    }

And then create a folder-country-conf in resources folder . Inside this folder create properties file which will have your configuration. Ex:

country.properties (Default)

country.name=United states

country_fr.properties

country.name=France

country.properties is the default properties if no country is sent in parameter and if you send country=fr in parameter then it will look for country_fr.properties file.

Now create a service that will get value from these properties file based on country parameter

@Service
public class CountryService {

    @Autowired
    private MessageSource messageSource;

    public String getMessage(String code) {
        Locale locale = LocaleContextHolder.getLocale();
        return this.messageSource.getMessage(code, null, locale);
    }
}

To test this autowire country service

@Autowired
CountryService countryService;

And then call getMessage method

countryService.getMessage("country.name");
like image 106
Ajit Soman Avatar answered Mar 09 '26 18:03

Ajit Soman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!