2017-09-10 12 views
2

国別設定ファイルを使用する春の起動アプリがあります。設定ファイルの構造は同じですが、値は国によって異なります。春の起動時にコンテクストの設定を読み込みます。

リソースディレクトリに各国のディレクトリを作成し、そこにその国の設定ファイルを配置しました。

要求パラメータで渡された国コードに基づいて、対応する設定ファイルを使用します。これを達成するための春のブート慣用的な方法は何ですか(別に、snakeyamlのようなものを使ってyaml設定ファイルを手動でロードすることは別ですか)?

おかげ

答えて

2

あなたはそれぞれの国のために春@Profileを使用することができます。 examplesおよびdocsを参照してください。

スプリングプロファイルを使用すると、アプリケーションの一部を分離し、特定の環境でのみ使用できるようにすることができます。

あなたは、実行時にプロファイルを追加することができます:どれ@Componentまたは@Configuration は、それがロードされたとき 制限する@Profileをマークすることができ

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); 
    ctx.getEnvironment().setActiveProfiles("za"); 
+0

私が理解する限り、起動時にどのプロファイルを読み込むかをspringブートに伝えなければなりません(例えば、dev、s検査など)。私の場合、それは例えば/ getData?countryCode = za、/ getData?countryCode = etcなどです。各リクエストを処理するために適切な設定ファイル(za/config.yamlとin/config.yaml)を使用する必要があります。プロファイルを使用してこれに最適なアプローチですか? – Vikk

+0

プログラムで追加することができます: 'AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.getEnvironment()。setActiveProfiles( "za"); ' – user7294900

5

あなたはMessageSourceためのBeanを作成することにより、acheiveすることができます、LocaleResolverLocaleChangeInterceptor Beanと、このようInterceptorRegistryLocaleChangeInterceptorを追加します。

@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()); 
     } 
    } 

そして、resourcesフォルダにcountry-confというフォルダを作成します。このフォルダの中に、あなたの設定を持つプロパティファイルを作成します。例:

country.properties(デフォルト)

country.name=United states 

country_fr.properties

country.name=France 

country.propertiesは、どの国がパラメータで送信されていない場合、デフォルトのプロパティであり、あなたがcountry=frを送信する場合パラメータではcountry_fr.propertiesファイルを探します。

さて、これらのプロパティの値がこのautowire国サービス

@Autowired 
CountryService countryService; 

をテストし

countryService.getMessage("country.name"); 
getMessageメソッドを呼び出すには、国のパラメータ

@Service 
public class CountryService { 

    @Autowired 
    private MessageSource messageSource; 

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

に基づいてファイルを取得するサービスを作成

関連する問題