Java 在 spring 上下文中通过类路径引用文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2525028/
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
Referencing a file via classpath in spring context
提问by Gandalf StormCrow
I'm trying to set org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
spring bean, I have jdbc.properties in src/main/config
.. when I put this file in src/main/resources
and put classpath:
in value my app deploys successfuly.
我正在尝试设置org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
spring bean,我在src/main/config
.. 中有 jdbc.properties,当我放入这个文件src/main/resources
并classpath:
赋值时,我的应用程序部署成功。
This works when jdbc.properties is located in src/main/resources
当 jdbc.properties 位于 src/main/resources
<property name="location" value="classpath:jdbc.properties" />
However I'm required to put any configuration inside src/main/config
, how do I point springs towards this location in the right way?
但是我需要在里面放置任何配置src/main/config
,我如何以正确的方式将弹簧指向这个位置?
采纳答案by stevedbrown
This is a classpath issue, not a Spring issue. Add src/main/config to your classpath and it will work. In Eclipse, this means adding it to the project Build Path->Source.
这是一个类路径问题,而不是 Spring 问题。将 src/main/config 添加到您的类路径中,它将起作用。在 Eclipse 中,这意味着将其添加到项目 Build Path->Source 中。
回答by ant
The right answer to this is given by @matt b,
@matt b给出了正确的答案,
"What I am referring to is the fact that when you package the application, the config files are not packaged in a folder named src/main/config in the packaged file (jar/war/etc.). Therefore your answer only works when you run the application within the source code, or when src/main/config is in the classpath (which it is not by default). The correct prefix is to use classpath: or another location."
“我所指的是这样一个事实,即当您打包应用程序时,配置文件并未打包在打包文件(jar/war/etc.)中名为 src/main/config 的文件夹中。因此,您的答案仅在以下情况下有效您在源代码中运行应用程序,或者当 src/main/config 在类路径中时(默认情况下不是)。正确的前缀是使用 classpath: 或其他位置。”
In this question :
在这个问题中:
回答by Karen
If it is a maven project, you can add
如果是maven项目,可以添加
<resources>
<resource>
<directory>src/main/config</directory>
<filtering>false</filtering>
</resource>
<resources>
to you <build>
in your pom.xml
<build>
在你的 pom.xml 中给你