gpt4 book ai didi

java - 如何读取 Java Spring MVC 应用程序中的属性(或任何其他文本)文件?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:39:43 26 4
gpt4 key购买 nike

我需要在我的 Spring MVC 应用程序中读取 java 属性文件,但我找不到这样做的方法。我在这里尝试了类似问题的几个答案,但没有成功。我是 Java 的新手,尤其是 Spring MVC,所以我可能搞砸了一些东西。

我不再确定该文件是否已成功部署。顺便说一句,我正在使用 Tomcat。

最佳答案

如果您使用的是 Spring 3.1+,则可以使用 @PropertySource注释:

@Configuration
@PropertySource("classpath:/com/example/app.properties")
public class AppConfig {
// create beans
}

或者对于基于 XML 的配置,您可以使用 <context:property-placeholder> :

<beans>
<context:property-placeholder location="classpath:com/example/app.properties"/>
<!-- bean declarations -->
</beans>

然后您可以使用 @Value 在属性文件中 Autowiring key 注释:

@Value("${property.key}") String propertyValue;

阅读 Spring reference docs 中的更多详细信息.

关于java - 如何读取 Java Spring MVC 应用程序中的属性(或任何其他文本)文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19252977/

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