[英]How to load Spring resource bundle from tomcat conf directory
I am trying to load the properties files located in the tomcat conf folder but the code below ends up causing a Missing Resource exception.If I use a property placeholder I can load properties files from tomcat conf fine. 我试图加载位于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>
If you're trying to externalize your resources and use ResourceBundle this is how I did it using ClassLoader. 如果您尝试外部化资源并使用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);
}
Now when I need to load the correct messages: ex: errors_en.properties located outside the application. 现在,当我需要加载正确的消息时:例如:位于应用程序外部的errors_en.properties。
ResourceBundle.getBundle("errors", requestLocale, loader);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.