我有一个“applicationContext.xml”文件,其中包含以下几行:
<bean id="jdbcPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/properties/support-center.properties</value>
</property>
</bean>
应用程序启动正常。但是当我打电话时:
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
它给出了一条错误消息:
原因:java.io.FileNotFoundException:类路径资源[WEB-INF/properties/support-center.properties]由于不存在而无法打开
所以,我想也许我可以将其更改为 <value>support-center.properties</value>
并将属性文件放在与 applicationContext.xml 相同的目录中,它可能会找到它,但没有,整个应用程序甚至无法启动,并说找不到 support-center.properties
现在我很困惑,因为原来的设置:/WEB-INF/properties/support-center.properties 是正确的,它使用此设置启动了应用程序,但为什么当我打电话时:
ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
找不到该属性?相同的设置,在启动时找到,但现在稍后。为什么?
编辑:
感谢您的回答,它有效[稍作修正],应该是:
<bean id="jdbcPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value ="classpath:support-center.properties" />
</bean>
最佳答案
假设 support-center.properties 位于同一目录中,或者位于您的类路径中 尝试使用
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value ="classpath:support-center.properties" />
</bean>
关于java - 为什么在启动时找到相同的属性文件,但后来在 Spring Java 应用程序中却找不到?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25172461/