Rad*_*FID 11 spring resourcebundle properties-file spring-boot
我正在使用MessageSourceSpring从.properties类路径中的文件加载错误消息.我的属性尊重某个"模板",例如{Object}.{field}.{unrespectedConstraint}:
userRegistrationDto.password.Size= Le mot de passe doit avoir au minimum 6 caractères.
userRegistrationDto.email.ValidEmail= Merci de saisir une addresse mail valide.
Run Code Online (Sandbox Code Playgroud)
在重构的情况下(例如,更改类的名称),我必须在几个地方更改我的属性文件.
有没有办法使用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.
Run Code Online (Sandbox Code Playgroud)
我认为这足以满足您的要求,如果您需要在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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4333 次 |
| 最近记录: |