如何读取 Java Spring MVC 应用程序中的属性(或任何其他文本)文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19252977/
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
How to read properties (or any other text) file in Java Spring MVC app?
提问by Eedoh
I need to read java properties file inside my Spring MVC app but I can't find the way to do that. I tried several answers from similar question here on SO, but I was unsuccessful. I'm new to Java, and especially Spring MVC so I probably messed up something.
我需要在我的 Spring MVC 应用程序中读取 java 属性文件,但我找不到这样做的方法。我在 SO 上尝试了类似问题的几个答案,但没有成功。我是 Java 新手,尤其是 Spring MVC,所以我可能搞砸了一些事情。
I'm not sure anymore that the file is being successfully deployed. I'm using Tomcat btw.
我不再确定该文件是否已成功部署。顺便说一句,我正在使用 Tomcat。
回答by gregwhitaker
You can have properties files automatically loaded in Spring by using the PropertySourcesPlaceholderConfigurer
.
您可以使用PropertySourcesPlaceholderConfigurer
.
Here is an example of configuring a PropertySourcesPlaceholderConfigurer
using Spring JavaConfig:
以下是PropertySourcesPlaceholderConfigurer
使用 Spring JavaConfig配置 a 的示例:
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer props = new PropertySourcesPlaceholderConfigurer();
props.setLocations(new Resource[] {
new ClassPathResource("/config/myconfig.properties"),
new ClassPathResource("version.properties")
});
}
This will load the properties from the files above on the classpath.
这将从类路径上的上述文件加载属性。
You can use these properties in property replacements within your application. For example, assume that there is a property in one of those files above named myprop
. You could inject myprop
's value into a field using the following:
您可以在应用程序中的属性替换中使用这些属性。例如,假设上面的这些文件之一中有一个名为myprop
. 您可以myprop
使用以下方法将的值注入到字段中:
@Value(${myprop})
private String someProperty;
You can also access the values of the properties by injecting Spring's Environment
object into your classes.
您还可以通过将 Spring 的Environment
对象注入到您的类中来访问属性的值。
@Resource
private Environment environment;
public void doSomething() {
String myPropValue = environment.getProperty("myprop");
}
In order to read any old file from within a web application the link that Frederic posted in the comments above provides a good explanation of the normal classloader hurdles one encounters when attempting to read files from within their war file and the solutions around it.
为了从 Web 应用程序中读取任何旧文件,Frederic 在上面的评论中发布的链接很好地解释了正常类加载器在尝试从其 war 文件中读取文件时遇到的障碍及其周围的解决方案。
回答by matsev
If you are using Spring 3.1+ you can use the @PropertySourceannotation:
如果您使用的是 Spring 3.1+,则可以使用@PropertySource注释:
@Configuration
@PropertySource("classpath:/com/example/app.properties")
public class AppConfig {
// create beans
}
or for XML-based configuration you can use the <context:property-placeholder>:
或者对于基于 XML 的配置,您可以使用<context:property-placeholder>:
<beans>
<context:property-placeholder location="classpath:com/example/app.properties"/>
<!-- bean declarations -->
</beans>
then you can autowire the key in the properties file using the @Valueannotation:
@Value("${property.key}") String propertyValue;
Read more details in the Spring reference docs.
在Spring 参考文档中阅读更多详细信息。
回答by Santosh Hegde
You can try the below code.
你可以试试下面的代码。
Add this to servelt-context.xml
将此添加到 servelt-context.xml
<context:property-placeholder location="classpath:config.properties"/>
And to access the contents of config file in java,
并在java中访问配置文件的内容,
@Value("${KEY}")
private String value;