java - 为什么在启动时找到相同的属性文件,但后来在 Spring Java 应用程序中却找不到?

标签 java spring configuration

我有一个“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/

相关文章:

java - 将 org.hibernate.jpa.HibernatePersistenceProvider 与 BoneCP 一起使用时遇到错误

c# - 在大项目中管理配置文件和代码常量

windows - 不同 Windows 上无法识别的属性 'multipleSiteBindingsEnabled' 问题

Java返回问题

java - 为什么我的 Spring Batch 应用程序无法处理?

java - 应用程序无法加载请求的类: com. mysql.cj.jdbc.Driver

spring - 从 Spring Shell 执行 shell 命令

c# - 如何使用 local.settings.json 文件的内容设置我的 Azure Function?

java - 在类里面硬编码设置不好吗?

java - 为什么 java.util.Stack 允许 .add(int, E)?