Dynamically load files on classpath using ReloadableResourceBundleMessageSource

∥☆過路亽.° 提交于 2019-11-29 07:54:29
JamesC

ReloadableResourceBundleMessageSource requires the files to be under the webapp/ directory, not in your classpath (it can't reload those).

martian111

You are on the right path of extending ReloadableResourceBundleMessageSource. As @JamesC mentioned, and per Spring's API documentation for ReloadableResourceBundleMessageSource:

Since application servers typically cache all files loaded from the classpath, it is necessary to store resources somewhere else (for example, in the "WEB-INF" directory of a web app).

I had a similar requirement as you. The key to solving it is extending ReloadableResourceBundleMessageSource and overriding the calculateAllFilenames(String basename, Locale locale) method to do more than appending language codes to the basename.

For example, if I have all my external message properties files under /srv/myapp/messages (in the filesystem, NOT classpath), the overridden method would do something like:

@Override
protected List<String> calculateAllFilenames(final String basename,
        final Locale locale) {

   return super.calculateAllFilenames("file:///srv/myapp/messages/" + basename, 
           locale);
}

In my case, my list of basenames are static and thus, I did not have to override other methods that calls calculateAllFilenames() for each base name.

In your specific case, I would use Company as the base name and override calculateAllFilenames() to scan a directory outside of your classpath to resolve actual basenames and then call super.calculateAllFilenames() for each resolved basename (i.e. super.calculateAllFilenames("file:///path/to/CompanyOneMessages", locale)). Potentially, something similar to https://stackoverflow.com/a/11223178/1034436 would help. Note: unlike the referenced answer, I would not do the directory scan as part of a property setter if the resolved list of base names can change dynamically. Of course, performance considerations for your app will also need to be weighed in as it would obviously drastically increase filesystem access.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!