spring - 为什么消息属性在 Spring MVC 中返回 Null
问题描述
我正在从事一个我想将消息外部化的项目。属性文件。使运营团队可以根据需要进行变更。
为此我写了一个这样的配置类
@Configuration
@ComponentScan(basePackages = {"lk.ideahub.symphony.modules", "lk.ideahub.symphony.product"})
@PropertySources(value = {@PropertySource(value = {"classpath:messages.properties"})})
public class MessageConfig {
private static final Logger log = LoggerFactory.getLogger(MessageConfig.class);
@Bean
public MessageSource messageSource() {
log.info("Message properties loading into the project");
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setCacheSeconds(10);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
在消息属性中
axipay.failure.account.already.exist=Sorry, the biller account number is already added.
在我的课上
public Payee myPayees{
//Asking the property into a variable
private static final String PAYEE_ACCOUNT_ALREADY_EXIST = "axipay.failure.account.already.exist";
@Autowired
Environment environment;
public void myMethod(){
String message = getEnvironment().getProperty(PAYEE_ACCOUNT_ALREADY_EXIST);
}
}
解决方案
@Autowired
private MessageSource messageSource;
//use this method to get message
public String getMessage(){
return messageSource.getMessage("axipay.failure.account.already.exist", null, new Locale("en"));
}
推荐阅读
- python - 使用 python-docx 将 QTextDocument 保存到 docx
- python - 如何从 onclick 获取模型 ID
- python - 提高 python pandas 速度的技术
- java - 在 Hibernate DDL 中删除外键
- vue.js - Vue.js - 按下按钮时发布方法不起作用
- mongodb - Mongo find 查询只返回一个结果
- python - 我正在尝试使用 Python 从 30 个类似的链接中抓取多个表
- python - 创建可执行文件后 GUI 不显示
- c# - 为什么我在更改 datapager 索引后丢失了过滤结果
- memory - 使用 /dev/mem 读取物理内存