簡體   English   中英

如何從tomcat conf目錄加載Spring資源包

[英]How to load Spring resource bundle from tomcat conf directory

我試圖加載位於tomcat conf文件夾中的屬性文件,但以下代碼最終導致缺少資源異常。如果使用屬性占位符,則可以從tomcat conf加載屬性文件。

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename">
            <value>file:${catalina.base}/conf/messages</value>

    </property>
</bean>

如果您嘗試外部化資源並使用ResourceBundle,這就是我使用ClassLoader做到的方式。

private static ClassLoader loader;

private static void setUp()
{
    String path = System.getProperty("catalina.base");
    File file = new File(path +"/conf/error_messages");
    URL[] urls = new URL[0];
    try {
        urls = new URL[]{file.toURI().toURL()};
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    loader = new URLClassLoader(urls);
}

現在,當我需要加載正確的消息時:例如:位於應用程序外部的errors_en.properties。

ResourceBundle.getBundle("errors", requestLocale, loader);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM