java - Maven项目: Spring cannot find property file inside src/main/resources using PropertyPlaceholderConfigurer

标签 java spring maven

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

这是我的 bean

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

Java:

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

提前谢谢大家。

最佳答案

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

<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 - Maven项目: Spring cannot find property file inside src/main/resources using PropertyPlaceholderConfigurer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19358300/

相关文章:

java - 将开放 ID 与 Google 应用引擎一起使用

java - 如何使用 JoinColumnn 选择少量数据进行显示

java - Bash 单引号数组元素并使用空格连接

maven - 在 Maven 中使用父模块共享代码和配置

java - 如何创建 mysql "sha-256"列?

java - 用于存储数据的适当数据结构

java - 在使用c3p0连接池配置的Mysql主从复制中偶尔会遇到到slave的通信链路故障

Spring webflux和从数据库读取

java - Spring安全库之间的差异

scala - 输出路径在同一模块之间共享错误