Hurelhuyag opened SPR-15113 and commented
I'm developing modular web application. I need to automatically register each module's messages.properties file to MessageSource. Currently I'm using below code. If MessageSource support wildcard it can be simple.
@Bean
public MessageSource messageSource() throws IOException {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader());
Resource[] resources = resolver.getResources("classpath*:com/mycompany/*/messages*.properties");
Set<String> baseNames = new HashSet<>();
baseNames.add("classpath:messages");
for (Resource resource : resources){
String path = resource.getURI().toString();
int i = path.indexOf("/WEB-INF/classes/") + 17;
int j = path.lastIndexOf('_');
if (j == -1){
j = path.lastIndexOf('.');
}
path = path.substring(i, j);
baseNames.add("classpath:"+path);
}
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames(baseNames.toArray(new String[baseNames.size()]));
messageSource.setCacheSeconds(5);
messageSource.setUseCodeAsDefaultMessage(true);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
No further details from SPR-15113
Comment From: snicoll
I don't understand what this is doing. The baseName
will already search in the classpath and that's a feature of every resource-based message source. If you want the base name to be dynamic based on the modules you have, then I think you should continue to find out about them and set them. I'd argue that exposing the name in some way is probably better than the path manipulation you're currently doing.