如何从tomcat conf目录

时间:2016-05-03 14:27:29

标签: spring tomcat resourcebundle

我正在尝试加载位于tomcat conf文件夹中的属性文件,但下面的代码最终会导致Missing Resource异常。如果我使用属性占位符,我可以从tomcat conf中加载属性文件。

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

    </property>
</bean>

1 个答案:

答案 0 :(得分:0)

如果您正在尝试外部化资源并使用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);
}

现在我需要加载正确的消息:ex:errors_en.properties位于应用程序之外。

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