使用ReloadableResourceBundleMessageSource在类路径上动态加载文件 [英] Dynamically load files on classpath using ReloadableResourceBundleMessageSource

查看:95
本文介绍了使用ReloadableResourceBundleMessageSource在类路径上动态加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我有一个Web应用程序(主要使用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文件中添加了以下内容:

 <!-无需重新启动网络应用程序即可重新加载资源包->< 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><属性名称="cacheSeconds" value ="1"/></bean><!-声明bean的位置以处理消息并定义属性引用我们将在整个表示层中使用它来引用消息属性->< bean id ="myappMessages" class ="com.myapp.resource.MyAPPMessages"><属性名称="messages" ref ="messages"/></bean> 

一切正常.

但是,它不能完全解决原始问题.每当我想向我们的应用程序中添加新公司时,都必须在applicationContext-config.xml文件中添加新行,然后重新部署/重新启动Web应用程序.

我希望能够简单地将新的公司属性文件拖放到属性tar中,并使其能够动态获取.

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

更新

这是我到目前为止所拥有的:

applicationContext-config.xml:

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

MyAPPReloadableResourceBundleMessageSource:

 包com.myapp.resource;导入org.springframework.context.support.ReloadableResourceBundleMessageSource;公共类MyAPPReloadableResourceBundleMessageSource扩展了ReloadableResourceBundleMessageSource{公共MyAPPReloadableResourceBundleMessageSource(){getResourceBundlesMessages();//简单的单个基名测试setBasename("classpath:/resource/dynamic/companyOneMessages");}@Override公共无效setBasename(String baesname){System.out.println("In setBasename");super.setBasename(baesname);}@Override公共无效setBasenames(String [] baesnames){System.out.println("In setBasenames");super.setBasenames(baesnames);}私有String [] getResourceBundlesMessages(){String [] propertiesFiles = null;//如何在com.resources.dynamic下获取带有.properties的所有文件名?(位置在类路径下)返回propertiesFiles;}} 

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

谢谢

托马斯

解决方案

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

I'm new to Spring and am attempting to use it's ReloadableResourceBundleMessageSource class.

I'm attempting to use it so that we no longer have to restart our web app for properties files changes/updates.

I have a web app (Primarily using JSF) and a separate tar component which contains all my properties files.

The structure of the properties tar is as follows:

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

This tar is unzipped and deployed to a location on the server which is specified as been on the classpath within the websphere configurations.

I added the following to my 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>

This all works fine.

BUT, it does not fully fix the original problem. Any time I want to add a new company to our application, I will have to add a new line to the applicationContext-config.xml file and redeploy/restart the web app.

I would like to be able to simply drop the new company properties file into the properties tar and for it to be dynamically picked up.

Is it possible to extend the ReloadableResourceBundleMessageSource class in such a way that it will search the class-path for properties files on application start-up and dynamically load all of them?

Update

This is what I have so far:

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;
    }
}

So all I need is how to get a list of all files under the classpath with .properties extension?

Thanks

Thomas

解决方案

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

这篇关于使用ReloadableResourceBundleMessageSource在类路径上动态加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆