Java Maven 项目:Spring 无法使用 PropertyPlaceholderConfigurer 在 src/main/resources 中找到属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19358300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:26:56  来源:igfitidea点击:

Maven Project: Spring cannot find property file inside src/main/resources using PropertyPlaceholderConfigurer

javaspringmaven

提问by royjavelosa

Spring cant find my property file (MyPropFile.properties) inside src/main/resources and throws an exception like below

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

But if I place MyPropFile.properties at the root of my project (MyProject/MyPropFile.properties) spring can find it and the programs executes properly.

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

How do I configure this so that I can place my .properties file inside src/main/resources

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

this is my namespace

这是我的命名空间

<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
    ">

this is my bean

这是我的豆子

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

Java:

爪哇:

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

Thanks in advance guys.

在此先感谢各位。

采纳答案by Debojit Saikia

Try this. Make this entry in your application config file:

尝试这个。在您的应用程序配置文件中输入以下内容:

<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>

and access the message property:

并访问消息属性:

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

回答by Brian Agnew

I'd expect Maven to copy this to the targetdirectory and for the classpath to be set appropriately. I wouldn't expect Maven to search the sourcedirectory other than at compile time.

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

回答by ryanp

You should use

你应该使用

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

Without the classpath:prefix, the PropertyPlaceholderConfigureris attempting to resolve the resource as a file resource, so is looking for it in your current working directory.

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

回答by tmarwen

As an addendum to @ryanpsolution which is the correct and sanitized way to deal with files resources, which should be under the classpathlocation.

作为@ryanp解决方案的附录,这是处理文件资源的正确且经过消毒的方法,文件资源应位于类路径位置下。

Maven will automagically collect the configured file resources and append them to your runtime classpathholder so that resources prefixed with classpath:can be resolved.

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

Otherwise, if you find your self stack with Maven not being able to stream file resources, you may hook into Maven's resources configuration and map your files location as follows:

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

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