Spring boot yml ResourceBundle file

前端 未结 2 607
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-04 05:21

I\'m using MessageSource of Spring to load errors messages from a .properties file in classpath. My properties respect a certain \"template\" such

2条回答
  •  我在风中等你
    2021-01-04 06:07

    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;
        }
    }
    

提交回复
热议问题