Java 如何在 Spring 中加载系统属性文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4116681/
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 load system properties file in Spring?
提问by Polaris878
I have a properties file which I would like loaded in to System Properties so that I can access it via System.getProperty("myProp")
. Currently, I'm trying to use the Spring <context:propert-placeholder/>
like so:
我有一个属性文件,我想将其加载到系统属性中,以便我可以通过System.getProperty("myProp")
. 目前,我正在尝试<context:propert-placeholder/>
像这样使用 Spring :
<context:property-placeholder location="/WEB-INF/properties/webServerProperties.properties" />
However, when I try to access my properties via System.getProperty("myProp")
I'm getting null
. My properties file looks like this:
但是,当我尝试通过System.getProperty("myProp")
获取null
. 我的属性文件如下所示:
myProp=hello world
How could I achieve this? I'm pretty sure I could set a runtime argument, however I'd like to avoid this.
我怎么能做到这一点?我很确定我可以设置运行时参数,但是我想避免这种情况。
Thanks!
谢谢!
采纳答案by Sean Patrick Floyd
While I subscribe to the Spirit of Bozho's answer, I recently also had a situation where I needed to set System Properties from Spring. Here's the class I came up with:
虽然我订阅了Bozho精神的回答,但我最近也遇到了需要从 Spring 设置系统属性的情况。这是我想出的课程:
Java Code:
Java代码:
public class SystemPropertiesReader{
private Collection<Resource> resources;
public void setResources(final Collection<Resource> resources){
this.resources = resources;
}
public void setResource(final Resource resource){
resources = Collections.singleton(resource);
}
@PostConstruct
public void applyProperties() throws Exception{
final Properties systemProperties = System.getProperties();
for(final Resource resource : resources){
final InputStream inputStream = resource.getInputStream();
try{
systemProperties.load(inputStream);
} finally{
// Guava
Closeables.closeQuietly(inputStream);
}
}
}
}
Spring Config:
弹簧配置:
<bean class="x.y.SystemPropertiesReader">
<!-- either a single .properties file -->
<property name="resource" value="classpath:dummy.properties" />
<!-- or a collection of .properties file -->
<property name="resources" value="classpath*:many.properties" />
<!-- but not both -->
</bean>
回答by Bozho
The point is to do this the other way around - i.e. use system properties in spring, rather than spring properties in the system.
关键是要以相反的方式做到这一点 - 即在 spring 中使用系统属性,而不是在系统中使用 spring 属性。
With PropertyPlaceholderConfigurer
you get your properties + the system properties accessible via the ${property.key}
syntax. In spring 3.0 you can inject these using the @Value
annotation.
随着PropertyPlaceholderConfigurer
您获得您的属性 + 可通过${property.key}
语法访问的系统属性。在 spring 3.0 中,您可以使用@Value
注释注入这些。
The idea is not to rely on calls to System.getProperty(..)
, but to instead inject your property values. So:
这个想法不是依赖于对 的调用System.getProperty(..)
,而是注入您的属性值。所以:
@Value("${foo.property}")
private String foo;
public void someMethod {
String path = getPath(foo);
//.. etc
}
rather than
而不是
public void someMethod {
String path = getPath(System.getProperty("your.property"));
//.. etc
}
Imagine you want to unit test your class - you'd have to prepopulate the System
object with properties. With the spring-way you'd just have to set some fields of the object.
想象一下,你想对你的类进行单元测试——你必须System
用属性预先填充对象。使用弹簧方式,您只需要设置对象的一些字段。
回答by Florin
In Spring 3 you can load system properties this way:
在 Spring 3 中,您可以通过以下方式加载系统属性:
<bean id="systemPropertiesLoader"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" value="#{@systemProperties}" />
<property name="targetMethod" value="putAll" />
<property name="arguments">
<util:properties location="file:///${user.home}/mySystemEnv.properties" />
</property>
</bean>