gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:42:29 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com