Does Spring MessageSource Support Multiple Class Path?

后端 未结 6 2201
终归单人心
终归单人心 2020-12-05 08:08

I am designing a plugin system for our web based application using Spring framework. Plugins are jars on classpath. So I am able to get sources such as jsp, see below

<
6条回答
  •  旧巷少年郎
    2020-12-05 08:15

    With the solution of @seralex-vi basenames /WEB-INF/messages did not function.

    I overwrited the method refreshProperties on the class ReloadableResourceBundleMessageSource wich perform both type of basenames (classpath*: and /WEB-INF/)

    public class SmReloadableResourceBundleMessageSource extends ReloadableResourceBundleMessageSource {
    
    private static final String PROPERTIES_SUFFIX = ".properties";
    
    private PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    
    @Override
    protected PropertiesHolder refreshProperties(String filename, PropertiesHolder propHolder) {
        if (filename.startsWith(PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX)) {
            return refreshClassPathProperties(filename, propHolder);
        } else {
            return super.refreshProperties(filename, propHolder);
        }
    }
    
    private PropertiesHolder refreshClassPathProperties(String filename, PropertiesHolder propHolder) {
        Properties properties = new Properties();
        long lastModified = -1;
        try {
          Resource[] resources = resolver.getResources(filename + PROPERTIES_SUFFIX);
          for (Resource resource : resources) {
            String sourcePath = resource.getURI().toString().replace(PROPERTIES_SUFFIX, "");
            PropertiesHolder holder = super.refreshProperties(sourcePath, propHolder);
            properties.putAll(holder.getProperties());
            if (lastModified < resource.lastModified())
              lastModified = resource.lastModified();
          }
        } catch (IOException ignored) { 
        }
        return new PropertiesHolder(properties, lastModified);
    }
    

    On the spring-context.xml you must have the classpath*: prefix

    
        
            
                /WEB-INF/i18n/enums
                /WEB-INF/i18n/messages
                classpath*:/META-INF/messages-common
                classpath*:/META-INF/enums
            
        
    
    

提交回复
热议问题