by Guest » 12 Feb 2025, 07:27
Ich habe eine Klasse reponemessageConfig, mit der die Eigenschaftendatei mit MessageSource < /p>
gelesen wird
Code: Select all
@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;
}
}
< /code>
Ich habe eine andere Klasse, die die MessageSource -Klasse wie unten automatisiert: < /p>
@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;
}
< /code>
Das Problem, mit dem ich ausgesetzt bin, ist, dass ich NoSuchMessageException erhalte, wenn ich versuche, von ReponemessageUtil mithilfe von GetMessage () -Methode zu lesen. Aber der gleiche Code funktioniert für mich in ResponseMessageConfig < /p>
Exception: "No message found under code '0400-0001.code' for locale 'en'."
Ich habe auch mit Locale.getDefault () versucht, aber kein Glück.
Ich habe eine Klasse reponemessageConfig, mit der die Eigenschaftendatei mit MessageSource < /p>
gelesen wird[code]@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;
}
}
< /code>
Ich habe eine andere Klasse, die die MessageSource -Klasse wie unten automatisiert: < /p>
@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;
}
< /code>
Das Problem, mit dem ich ausgesetzt bin, ist, dass ich NoSuchMessageException erhalte, wenn ich versuche, von ReponemessageUtil mithilfe von GetMessage () -Methode zu lesen. Aber der gleiche Code funktioniert für mich in ResponseMessageConfig < /p>
Exception: "No message found under code '0400-0001.code' for locale 'en'."
[/code]
Ich habe auch mit Locale.getDefault () versucht, aber kein Glück.