spring boot ResourceBundle无效。有什么诀窍

时间:2017-01-10 00:53:28

标签: java spring internationalization

有人可以解释我缺少的东西吗?

这些例子很简单。在最基本的应用程序中加载很好,我可以得到我的属性:

@SpringBootApplication
public class SampleAmqpSimpleApplication {


    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(SampleAmqpSimpleApplication.class, args);
        String foo = context.getMessage("foo.name", null, Locale.US);
        System.out.println("*********** " +foo);
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
        resourceBundleMessageSource.setBasename("messages");
        return resourceBundleMessageSource;
    }

}

输出:

  

*********** baz

但在我的实际应用中:

@Configuration
@SpringBootApplication
@EnableAsync
@EnableCaching
@EnableWebSocket
@EnableAspectJAutoProxy
@EnableReactor
public class TheOracle {

        public static void main(String[] args) throws IOException {
    ConfigurableApplicationContext ac = SpringApplication.run(TheOracle.class, args);
    InputStream friendly = TheOracle.class.getResourceAsStream("/friendly_en_US.properties");
    if(friendly == null)
        throw new RuntimeException();

    String message = null;
    try {
        message = ac.getMessage("foo.name", null, Locale.US);
    } catch (NoSuchMessageException e) {
        System.out.println("No message");
        Properties props = new Properties();
        props.load(friendly);
        message = props.getProperty("foo.name");
    }
    System.out.println(message);
}

@Bean
public MessageSource friendlyNames() {
    ResourceBundleMessageSource bundle = new ResourceBundleMessageSource();
    bundle.setBasename("friendly");
    return bundle;
}
}

产生:

  

16:49:22.555 [main] INFO com.mz.matrix.TheOracle - 启动TheOracle   在10.552秒(JVM运行12.993)   没有消息   巴兹

原始例外:

  

线程“main”中的异常   org.springframework.context.NoSuchMessageException:未找到任何消息   在代码'foo.name'下,用于locale'en_US'。在   org.springframework.context.support.DelegatingMessageSource.getMessage(DelegatingMessageSource.java:69)     在   org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:1232)     在com.mz.matrix.TheOracle.main(TheOracle.java:58)

0 个答案:

没有答案