这与一个老问题非常相似,但解决方案对我不起作用。
我有一个 WAR 包。
在 web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
在 application-context.xml
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:social.properties</value>
</property>
</bean>
但是得到这个:
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/social.properties]
我检查了 WAR 包 - .xml
和 .properties
文件都在 /WEB-INF/classes
.properties
文件在 src/main/resources
和 .xml
在 src/main/java
(在默认包中)和maven在默认包中正确传输它们(我认为) WEB-INF/classes
有谁知道我为什么会得到这个异常?谢谢。
编辑:我只想补充一点,JUnit 测试正常运行(我的意思是它们从 social.properties
加载了它们应该加载的内容)但是在运行应用程序时它会忽略我的 classpath:
前缀
最佳答案
不要使用类路径。这可能会导致不同的 ClassLoader(容器与应用程序)出现问题。 WEB-INF 永远是更好的选择。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
和
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/social.properties</value>
</property>
</bean>
关于java - 无法打开 ServletContext 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6035464/