title: Spring boot - different messages.properties based on request
body: <p>I would like to load a different set of messages_xx.properties based on the HttpServletRequest to differentiate them based on our customers.</p>
  
  <p>In templates and through all the application, we have a @Bean which gives the actual customer based on the path of the request</p>
  
  <pre><code>@Component
  public class CompanySelector {
  
      @Autowired
      private ICompanyService service;
  
      public String getURLBase(HttpServletRequest request) throws MalformedURLException {
          URL requestURL = new URL(request.getRequestURL().toString());
          String port = requestURL.getPort() == -1 ? "" : ":" + requestURL.getPort();
          return requestURL.getHost() + port;
      }
  
      public Company getActualCompany(HttpServletRequest request) throws MalformedURLException{
          String url = getURLBase(request);
  
          Company company = service.findByCompanyUrl(url);
          if(company != null){
              return company;
          }
          return null;
      }
  
  }
  </code></pre>
  
  <p>Now, we configure the <code>MessageSource</code> in <code>WebConfig</code> which extends <code>WebMvcConfigurerAdapter</code> and we would like to do something like that</p>
  
  <pre><code>@Configuration
  @ComponentScan("it.besmart.eshare.web")
  public class WebConfig extends WebMvcConfigurerAdapter{
  
      public WebConfig(){
          super();
      }
  
      @Autowired
      CompanySelector companySelector;
  
      @Autowired
      HttpServletRequest request;
  
      @Bean
      public MessageSource messageSource() {
          ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
          Company company = null;
          try {
              company = companySelector.getActualCompany(request);
          } catch (MalformedURLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          if (company != null){
              messageSource.setBasename("classpath:"+ company.getSlug() + "_messages");
          } else {
              messageSource.setBasename("classpath:messages");
          }
          messageSource.setDefaultEncoding("UTF-8");
  
          return messageSource;
      }
  
  }
  </code></pre>
  
  <p>but obviously it doesn't work because we don't have a request during configuration... Is there another way to load the messages file based on the request? Or any other best practice to adopt? Because our other choice would be using only one file per language and using the company.getSlug() at the beginning of each phrase, but we would decuplicate the size of file...</p>