【问题标题】:Maven Project: Spring cannot find property file inside src/main/resources using PropertyPlaceholderConfigurerMaven 项目:Spring 无法使用 PropertyPlaceholderConfigurer 在 src/main/resources 中找到属性文件
【发布时间】:2013-10-21 21:42:18
【问题描述】:

Spring 在 src/main/resources 中找不到我的属性文件 (MyPropFile.properties) 并引发如下异常

java.io.FileNotFoundException: class path resource [file*:/src/main/resources/MyPropFile.properties] cannot be opened because it does not exist

但是如果我将 MyPropFile.properties 放在我的项目的根目录(MyProject/MyPropFile.properties),spring 可以找到它并且程序可以正常执行。

如何配置它以便我可以将 .properties 文件放在 src/main/resources 中

这是我的命名空间

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">

这是我的豆子

<context:property-placeholder location="classpath:MyPropFile.properties" />

Java:

@Value("${message.fromfile}")
      private String message;

提前谢谢各位。

【问题讨论】:

    标签: java spring maven


    【解决方案1】:

    试试这个。在您的应用程序配置文件中创建此条目:

    <beans xmlns:context="http://www.springframework.org/schema/context"
     .....
     xsi:schemaLocation="...
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
     ....>
    
    <context:property-placeholder location="classpath:MyPropFile.properties" />
     ....
    </beans>
    

    并访问消息属性:

    @Value("${messageFromfile}")
    private String message;
    

    【讨论】:

    • 我已经更新了我的问题,反映了先生您建议的更改。不幸的是它仍然找不到属性文件 java.io.FileNotFoundException: MyPropFile.properties (系统找不到指定的文件)
    • 注释掉PropertyPlaceholderConfigurer bean 定义并尝试构建和运行您的应用程序。
    • 只有在 MyPropFile.properties 位于我项目的根目录时才有效。我希望它即使在 src/main/resources/MyPropFile.properties 也能工作
    • 你删除了PropertyPlaceholderConfigurer吗?
    • 是的,我还更新了我的问题以反映对我的 bean 的更改。即使我使用我的 IDE 运行此方法,它是否有效?还是仅当项目在 jar 中编译/打包时才有效?因为我正在使用 Eclipse/STS 右键单击​​运行我的项目-> 运行为
    【解决方案2】:

    我希望 Maven 将其复制到 target 目录并适当设置类路径。我不希望 Maven 在编译时搜索 source 目录。

    【讨论】:

    • 什么意思?如果我将属性文件放在项目的根目录下,它在 Eclipse 中运行良好。我的问题是当我将属性文件放在 src/main/resources 时,这应该是 maven 项目的默认资源位置。
    【解决方案3】:

    你应该使用

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
                <value>classpath:/MyPropFile.properties</value>
            </property>
    </bean>
    

    如果没有 classpath: 前缀,PropertyPlaceholderConfigurer 会尝试将资源解析为文件资源,因此正在您当前的工作目录中查找它。

    【讨论】:

    • 刚刚尝试过,但它给出了这个错误。顺便说一句,我在 java.io.FileInputStream.open(Native Method) 使用 Spring 3.0.5.RELEASE java.io.FileNotFoundException: MyPropFile.properties (系统找不到指定的文件)
    • 您能验证{project directory}/target/classes/MyPropFile.properties 存在吗?
    【解决方案4】:

    作为 @ryanp 解决方案的补充,这是处理文件资源的正确且经过清理的方法,应位于 classpath 位置下。

    Maven 将自动收集配置的文件资源并将它们附加到您的运行时 classpath 持有者,以便可以解析以 classpath: 为前缀的资源.

    否则,如果您发现自己的 Maven 堆栈无法流式传输文件资源,您可以挂钩到 Maven 的资源配置并映射您的文件位置,如下所示:

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 2020-07-24
      相关资源
      最近更新 更多