[英]How to load any file present under the conf or resources directory in 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.