Java,Spring,无法找到/WEB-INF/spring.properties 我需要在propertyConfigurer之外的其他地方设置它吗?
我收到一条错误消息:无法加载属性;嵌套异常是java.io.FileNotFoundException:类路径资源[WEB-INF/spring.properties]无法打开,因为它不存在
。 spring.properties 文件确实存在并且位于我的 /WEB-INF 目录中(我已确认在构建项目后它位于我的构建目录中)。我将它设置在我的项目的 .classpath 目录中,如下所示:
<classpathentry kind="src" path="src/main/webapp/WEB-INF/spring.properties"/>
在我的 Spring 应用程序上下文中,我将其输入如下:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/spring.properties" />
</bean>
如果这是一个基本问题,我很抱歉,但我真的很困惑问题是什么以及如何解决它,我对此进行了很多搜索,但似乎无法弄清楚。感谢您的任何建议
I am getting an error message that Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/spring.properties] cannot be opened because it does not exist
. The spring.properties files does exist and is in my /WEB-INF directory (I have confirmed that it is in my build directory after building the project). I have it set on my project's .classpath directory like this:
<classpathentry kind="src" path="src/main/webapp/WEB-INF/spring.properties"/>
In my Spring application context, I have it entered like this:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/spring.properties" />
</bean>
I apologize if this is a basic question but I really am perplexed as to what the problem is and how to solve it, I have done a lot of searching on this and can't seem to figure it out. Thanks for any advice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
查看我的一个使用
PropertyPlaceholderConfigurer
的 Web 应用程序,我发现我将属性放入/WEB-INF/classes
中,然后配置 PPC 以将其与 Spring 一起使用类路径:
URL;即访问为
Looking at one of my webapps that uses a
PropertyPlaceholderConfigurer
, I see that I put the properties in/WEB-INF/classes
and then configured the PPC to use it with a Springclasspath:
URL; i.e.accessed as
Spring 支持 ServletContextResource,您可以通过完全省略资源前缀来使用它。 “您将返回适合该特定应用程序上下文的资源类型”,并且由于我们使用的是 Web 上下文,因此该资源将是 ServletContextResource。
该路径来自您的网络应用程序的根目录。我们的路径看起来像
Spring supports a ServletContextResource, which you can use by leaving off the resource prefix entirely. "You will get back a Resource type that is appropriate to that particular application context", and since we are using a web context, that resource will be a ServletContextResource.
The path is from the root of your webapp. Our paths look like
您的路径(“src/main/webapp”)表明您正在使用 Maven 来构建项目。如果是这种情况,请将您的 .properties 文件放入 /src/main/resources 并使用“
classpath:
”来访问它们,即 src/main/resources 下的所有内容应该可以通过类路径访问,无需任何进一步的配置。Your path ("src/main/webapp") suggests you are using Maven to build the project. If this is the case, put your .properties -file(s) to /src/main/resources and use "
classpath:<filename>
" to access them, everything under src/main/resources should be accessible through classpath without any further configuration.尝试将该文件放在
WEB-INF/classes/
下,并使用value="spring.properties"
引用它。我认为这应该可以解决问题。Try putting the file under
WEB-INF/classes/
and referring to it withvalue="spring.properties"
. I think that should do the trick.只需将 spring.properties 文件放在 src/main/webapp 目录下(与 WEB-INF 一起)并使用
Just put the spring.properties file under the directory src/main/webapp (alongside WEB-INF) and referring to it with