无法打开ServletContext资源 [英] Could not open ServletContext resource
问题描述
我有一个WAR包。
$这个问题与一个较老的问题非常类似,但解决方案对我无效。 b $ b
在 web.xml中
<上下文-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 =placeholderConfigclass =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer>
< property name =location>
< value> classpath:social.properties< / value>
< / property>
< / bean>
但是得到这个:
org.springframework.beans.factory.BeanInitializationException:无法加载属性;嵌套的异常是java.io.FileNotFoundException:无法打开ServletContext资源[/social.properties]
我检查了WAR包 - code> .xml 和 .properties
文件都位于 / WEB-INF / classes $ c
(在默认的包中)和maven传输它们(c $ c>和
$ b .properties
文件位于 src / main / resources
src / main / java .xml
我认为)正确地在 WEB-INF / classes
的默认包中有没有人知道为什么我可以得到这个例外?谢谢。
编辑:我只是想补充JUnit测试正确(我的意思是他们加载他们应该从 social.properties
),但运行应用程序时忽略了我的
classpath:
前缀
不要使用classpath。这可能会导致不同ClassLoaders(容器与应用程序)的问题。 WEB-INF总是最好的选择。
< context-param>
< param-name> contextConfigLocation< / param-name>
< param-value> /WEB-INF/spring-config.xml< / param-value>
< / context-param>
和
< bean id =placeholderConfigclass =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer>
< property name =location>
<值> /WEB-INF/social.properties< /值>
< / property>
< / bean>
This is quite similar question to one older but the solution did not work for me.
I have a WAR package.
In 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>
In application-context.xml
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:social.properties</value>
</property>
</bean>
But getting this:
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/social.properties]
I checked the WAR package - .xml
and .properties
files are both in /WEB-INF/classes
.properties
file is in src/main/resources
and .xml
in src/main/java
(in default package both) and maven transports them (I think) correctly in the default package of WEB-INF/classes
Does anyone know why i could get this exception? Thank you.
EDIT: I just want to add that JUnit tests goes correctly (i mean they load what they should from social.properties
) but when running the app it ignores my classpath:
prefix
Do not use classpath. This may cause problems with different ClassLoaders (container vs. application). WEB-INF is always the better choice.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
and
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/social.properties</value>
</property>
</bean>
这篇关于无法打开ServletContext资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!