java - 使用 ReloadableResourceBundleMessageSource 在类路径上动态加载文件

标签 java spring-mvc resourcebundle

我是 Spring 的新手,正在尝试使用它的 ReloadableResourceBundleMessageSource 类。

我正在尝试使用它,以便我们不再需要为属性文件更改/更新重新启动我们的网络应用程序。

我有一个网络应用程序(主要使用 JSF)和一个包含我所有属性文件的单独的 tar 组件。

属性tar的结构如下:

 - CompanyOneMessages.properties
 - CompanyOneMessages_fr_FR.properties
 - CompanyTwoMessages.properties
 - CompanyTwoMessages_fr_FR.properties
 - CompanyThreeMessages.properties
 - CompanyThreeMessages_fr_FR.properties
 - ...

此 tar 被解压缩并部署到服务器上指定为在 websphere 配置中的类路径上的位置。

我将以下内容添加到我的 applicationContext-config.xml 中:

<!-- Enable reloading of resource bundles without requiring web-app restart -->
    <bean id="messages"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>  
                <value>classpath:com/resource/dynamic/CompanyOneMessages</value>
                <value>classpath:com/resource/dynamic/CompanyTwoMessages</value>
                <value>classpath:com/resource/dynamic/CompanyThreeMessages</value>              
            </list>
        </property>     
        <property name="cacheSeconds" value="1" />
    </bean>

    <!-- Declare location of bean to handle messages and define property reference 
         we will use to reference message properties throughout the presentation layer -->
    <bean id="myappMessages" class="com.myapp.resource.MyAPPMessages">
        <property name="messages" ref="messages" />
    </bean>

一切正常。

但是,它并没有完全解决原来的问题。 任何时候我想向我们的应用程序添加新公司时,我都必须向 applicationContext-config.xml 文件添加新行并重新部署/重新启动 Web 应用程序。

我希望能够简单地将新的公司属性文件放入属性 tar 中,以便动态获取它。

是否可以扩展 ReloadableResourceBundleMessageSource 类,使其在应用程序启动时搜索属性文件的类路径并动态加载所有这些文件?

更新

这是我目前所拥有的:

applicationContext-config.xml:

<bean id="messages" class="com.resource.MyAPPReloadableResourceBundleMessageSource">
</bean>

MyAPPReloadableResourceBundleMessageSource:

package com.myapp.resource;

import org.springframework.context.support.ReloadableResourceBundleMessageSource;

public class MyAPPReloadableResourceBundleMessageSource extends ReloadableResourceBundleMessageSource
{   
    public MyAPPReloadableResourceBundleMessageSource()
    {   
        getResourceBundlesMessages();

        // Simply single basename test
        setBasename("classpath:/resource/dynamic/companyOneMessages");      
    }

    @Override
    public void setBasename(String baesname)
    {
        System.out.println("In setBasename");
        super.setBasename(baesname);
    }

    @Override
    public void setBasenames(String[] baesnames)
    {
        System.out.println("In setBasenames");
        super.setBasenames(baesnames);
    }

    private String[] getResourceBundlesMessages()
    {
        String[] propertiesFiles = null;

        // How do I get all filenames with .properties under com.resources.dynamic? (location is under classpath)

        return propertiesFiles;
    }
}

所以我只需要如何获取类路径下所有扩展名为 .properties 的文件的列表?

谢谢

托马斯

最佳答案

ReloadableResourceBundleMessageSource 要求文件位于 webapp/目录下,而不是在您的类路径中(它无法重新加载这些文件)。

关于java - 使用 ReloadableResourceBundleMessageSource 在类路径上动态加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9035588/

相关文章:

java - Java中两个List的比较以及使用JSTL在JSP中以表格格式显示两个List之间的差异

java - Grails,我如何获得一个不保存的对象

java - Spring Cloud微服务服务器端口强制转换异常

java - 向 Java 类添加方法是否会增加其实例的内存使用量?

java - 在 Spring MVC 中映射/(root URL)

java - : tag in the Spring template app? bean 是什么

java - 在不同的 PC 上找不到 ResourceBundle

java - 从 jar 中加载 ResourceBundle

java - 资源包java

java - Spring MVC : Threads not executing in production