【问题标题】:How to load Spring resource bundle from tomcat conf directory如何从tomcat conf目录加载Spring资源包
【发布时间】:2016-08-28 15:57:02
【问题描述】:
我正在尝试加载位于 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>
【问题讨论】:
标签:
spring
tomcat
resourcebundle
【解决方案1】:
如果您尝试将资源外部化并使用 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);