【发布时间】:2017-11-18 08:12:52
【问题描述】:
我需要我的 Web 服务来为我提供包含 JSON 格式的本地化文本的 messages.properties。我知道我可以编写自己的解析器来做到这一点,但是我应该在 Spring 框架的哪里插入这个逻辑?或者是否有 Spring 基础设施功能已经可以做到这一点?
【问题讨论】:
标签: java json spring spring-mvc properties
我需要我的 Web 服务来为我提供包含 JSON 格式的本地化文本的 messages.properties。我知道我可以编写自己的解析器来做到这一点,但是我应该在 Spring 框架的哪里插入这个逻辑?或者是否有 Spring 基础设施功能已经可以做到这一点?
【问题讨论】:
标签: java json spring spring-mvc properties
您可以在您的类上使用@PropertySource 注解将您的属性文件加载到内存中。
@Configuration
class MessagesConfiguration {
@Bean(name = "messageProperties")
public static PropertiesFactoryBean mapper() {
PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource("messages.properties"));
return bean;
}
@Resource(name="messageProperties")
private Properties messages = new Properties();
public Properties getMessages() {
return messages;
}
}
Properties.class 只是 Map<String, String> 的包装器,因此您可以将其转换为 JSON。
【讨论】:
messages 启动所有属性。它会