![](/img/trans.png)
[英]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.