Spring Boot:使用多个locale message.properties进行日志记录,具体取决于' args'值

时间:2016-08-29 08:19:55

标签: java spring-boot

我在春季靴子里有点新鲜。我想设置我的spring boot aplication,以便在启动程序时使用特定的arg时选择特定的locale messages.properties文件。这些message.properties字段将用于记录程序事件以及向客户发送电子邮件报告。

我发现的示例仅适用于Web应用程序。我无法通过内置弹簧启动技术找到有关如何实现这一目标的任何合适示例。

1 个答案:

答案 0 :(得分:1)

好吧,常规Spring Boot只是一个从main()方法运行的应用程序 - 您可以在运行Spring应用程序之前通过Locale.setDefault方法设置默认的Locale,如下所示:

@SpringBootApplication
public class DemoApplication {

  public static void main(String[] args) {
    Locale defaultLocale = determineLocaleFrom(args);
    Locale.setDefault(defaultLocale);

    ApplicationContext context = SpringApplication.run(DemoApplication.class, args);

    MessageSource messageSource = context.getBean(MessageSource.class);
    // when fetching messages, always read from the default locale
    messageSource.getMessage("my.message.code", null, Locale.getDefault());
  }

}