不可能使用SpringBoot读取消息程序的消息

问题描述 投票:0回答:1
我有一个ResponseMessageConfig,它将使用MessagsOurce

读取属性文件。

@Configuration public class ResponseMessageConfig { @Value("${response-message.path:}") String messageFilePath; @Value("${response-message.cache:}") Integer cacheInSeconds; @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); // Load from external path (e.g., /config/messages.properties) messageSource.setBasename(messageFilePath); // Set encoding to avoid issues with special characters messageSource.setDefaultEncoding("UTF-8"); // Reload messages every "cacheInSeconds" seconds messageSource.setCacheMillis(cacheInSeconds); String msg = messageSource.getMessage(String.format("%s.%s", "0400-0001", "code"), null, Locale.ENGLISH); return messageSource; } }
我有另一个类,可以自动化MessageRirce类,如下所示:

@Slf4j @Component public class ResponseMessageUtil { @Autowired private MessageSource messageSource; private static ResponseMessageUtil instance; @PostConstruct private void init() { instance = this; } public static String getMessage(String status, Object[] args) { String msg = instance.messageSource.getMessage(String.format("%s.%s", status, "code"), args, Locale.ENGLISH); return msg; }
我面临的问题是,我尝试使用getMessage()方法从Responsemessageutil读取时会获得nosuchmessageException。但是同一代码在ReppesteMessageConfig

中对我有用

Exception: "No message found under code '0400-0001.code' for locale 'en'."
我也尝试了LoCale.GetDefault(),但没有运气。
    

try以将其定义为
java spring-boot
1个回答
0
投票

cacheInSeconds

作为豆类创造方法的参数,以使它们不会迟到。不确定它会有所帮助,但至少它会使依赖关系明确到春天。
当然,将依赖项放在静态字段中是一个不好的模式...
	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.