Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring启动yml ResourceBundle文件_Spring_Spring Boot_Properties File_Resourcebundle - Fatal编程技术网

Spring启动yml ResourceBundle文件

Spring启动yml ResourceBundle文件,spring,spring-boot,properties-file,resourcebundle,Spring,Spring Boot,Properties File,Resourcebundle,我正在使用Spring的MessageSource从类路径中的.properties文件加载错误消息。我的属性尊重某个“模板”,例如{Object}.{field}.{unspectedconstraint}示例: userRegistrationDto.password.Size= Le mot de passe doit avoir au minimum 6 caractères. userRegistrationDto.email.ValidEmail= Merci de saisir u

我正在使用Spring的
MessageSource
从类路径中的
.properties
文件加载错误消息。我的属性尊重某个“模板”,例如
{Object}.{field}.{unspectedconstraint}
示例:

userRegistrationDto.password.Size= Le mot de passe doit avoir au minimum 6 caractères.
userRegistrationDto.email.ValidEmail= Merci de saisir une addresse mail valide.
在重构的情况下(例如更改类名),我必须在几个地方更改属性文件

有没有办法将yaml文件(messages.yml)用作ResourceBundle,以获得以下内容:

userRegistrationDto:
  password:
    Size: Le mot de passe doit avoir au minimum 6 caractères.
  email:
    ValidEmail: Merci de saisir une addresse mail valide.

我找到的最佳解决方案是@vtosh:to use。唯一的问题(但仍然)是它不够受欢迎

另一个选项是手动扩展
ResourceBundle.Control
类来扩展Java本地化支持(您可以找到一个官方示例)。但我认为这没有什么意义,因为@vtosh-found库使用了这种方法


为什么春天没有解决方案?好吧,你可以从中找到答案。它仍然处于开放状态,所以我不希望他们有任何解决方案,至少现在是这样。

我认为这应该足以满足您的需求,如果您需要在VM操作期间重新加载MessageSource,您可能需要做更多的挖掘

@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;
    }
}
好吧,有(免责声明:我自己没有测试过):