java 如何在不重新启动 Tomcat 的情况下重新加载属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3573575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:28:55  来源:igfitidea点击:

How to reload properties file without rebooting Tomcat

javatomcatclassloader

提问by Albataur

I load a property file from classpath with the method :

我使用以下方法从类路径加载属性文件:

    String cheminFichier = new StringBuilder(100).append(classeBP.getPackage().getName().replace(".", "/")).append(File.separator).append(
        REPERTtheitroadE_MAPPING).append(nomFichier).append(".properties").toString();
    InputStream isMapping = Thread.currentThread().getContextClassLoader().getResourceAsStream(cheminFichier.toString());
    if (isMapping == null)
    {
        throw new ServiceMappingException("Erreur lors du chargement du mapping du service. Le fichier "
            + cheminFichier + " n'existe pas.");
    }
    else
    {
        Properties mapping = new Properties();
        try
        {
            mapping.load(isMapping);
        }
        catch (IOException e)
        ...
    }

Ok, it's work. But if I modify the content of the property file when Tomcat is running, changes are ignored. It's not hot-reloaded as with classes.

好的,它的工作。但是如果我在 Tomcat 运行时修改属性文件的内容,更改将被忽略。它不像类那样热重载。

My context is configured with reloadable="true"option and the classloader returned by Thread.currentThread().getContextClassLoader() is the WEBAPP classloader (not the system classloader or other).

我的上下文配置了reloadable="true"选项,Thread.currentThread().getContextClassLoader() 返回的类加载器是 WEBAPP 类加载器(不是系统类加载器或其他)。

I read it's possible to use ServletContext.getResourceAsStream, but I haven't access to the servlet context.

我读到可以使用 ServletContext.getResourceAsStream,但我无法访问 servlet 上下文。

It's Tomcat 5.5.

这是 Tomcat 5.5。

Any idea ? If not, do you have a solution for forcing to reload a specific resource (my property file) ?

任何的想法 ?如果没有,您是否有强制重新加载特定资源(我的属性文件)的解决方案?

Thanks !

谢谢 !

采纳答案by Stephen C

Actually, your webapp does have access to the ServletContext at various points. For example you can get it using ServletRequest.getServletContext(), or from a ServletContextEventobtained via a ServletContextListenercallback.

实际上,您的 web 应用程序确实可以在各个点访问 ServletContext。例如,您可以使用ServletRequest.getServletContext(),或从ServletContextEvent通过ServletContextListener回调获得的获取它。



This is a bit off topic, but you should realise that this code:

这有点离题,但你应该意识到这段代码:

String cheminFichier = new StringBuilder(100).append(classeBP.getPackage().getName().replace(".", "/")).append(File.separator).append(
    REPERTtheitroadE_MAPPING).append(nomFichier).append(".properties").toString();

could / should be written as:

可以/应该写成:

String cheminFichier = classeBP.getPackage().getName().replace(".", "/")) +
    File.separator + REPERTtheitroadE_MAPPING + nomFichier + ".properties";

There is no performance advantage herein explicitly using a StringBuilderhere, because the Java compiler will compile the concatenation to the same code.

没有性能上的优势,这里在明确使用StringBuilder在这里,因为Java编译器将编译级联到相同的代码。

You only need to use a StringBuilderif the concatenation involves a loop.

StringBuilder如果串联涉及循环,您只需要使用 a 。

回答by Pascal Thivent

You could use something like Commons Configurationthat supports automatic reloading.

您可以使用诸如支持自动重新加载的Commons Configuration 之类的东西。

回答by Colin Hebert

You can still use the Tomcat Manager to reload your application.

您仍然可以使用 Tomcat 管理器重新加载您的应用程序。