Spring boot yml ResourceBundle file

有些话、适合烂在心里 提交于 2019-12-01 03:08:01

I think this should suffice for your requirements, if you need the MessageSource to be reloadable during VM operation, you might have to do a bit more digging.

@Configuration
public class TestConfig {

    @Bean(name = "testProperties")
    public Properties yamlProperties() throws IOException {
        YamlPropertiesFactoryBean bean = new YamlPropertiesFactoryBean();
        bean.setResources(new ClassPathResource("test.yml"));
        return bean.getObject();
    }

    @Bean
    public MessageSource messageSource() throws IOException {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setCommonMessages(yamlProperties());
        return messageSource;
    }
}

The best solution I managed to find was found before me by @vtosh: to use this library. The only problem (but still) is that it is not popular enough.

The other option could be to extend Java localization support manually extending the ResourceBundle.Control class (you can find an official example here). But I don't see much sense in it since the library @vtosh found uses this approach.

Why there is no a solution for Spring? Well, the answer you can find in this jira. It is still in Open state so I don't expect to be any solution from their side at least for now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!