Java 我可以为 Spring FileSystemResource 使用基于环境变量的位置吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1841857/
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
Can I use an Environment variable based location for Spring FileSystemResource?
提问by
I have a requirement to have all our properties files be stored in a directory. The location of this directory should be stored in a system environment variable. In my application context I will need to access this environment variable to create the FileSystemResource bean. Here is an example of what I would normally have:
我需要将我们所有的属性文件都存储在一个目录中。此目录的位置应存储在系统环境变量中。在我的应用程序上下文中,我需要访问这个环境变量来创建 FileSystemResource bean。这是我通常拥有的示例:
<bean id="properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<bean class="org.springframework.core.io.FileSystemResource">
<constructor-arg>
<value>myprops.properties</value>
</constructor-arg>
</bean>
</property>
</bean>
Instead I will need to have it be something like
相反,我需要让它像
<value>${prop_file_location}/myprops.properties</value>
Where prop file location is an environment variable. Does anyone know an easy way of doing this?
其中 prop 文件位置是一个环境变量。有谁知道这样做的简单方法?
I am using spring 2.5.6 and java 1.6
我正在使用 spring 2.5.6 和 java 1.6
采纳答案by Sanjay Ginde
UPDATE
更新
We later upgraded to Spring 3.0.X and we were able to take advantage of the spring expression language. Our approach simplified from three beans to the following snippet:
我们后来升级到 Spring 3.0.X,我们能够利用 spring 表达式语言。我们的方法从三个 bean 简化为以下代码段:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:defaults.properties</value>
<value>file:/a/defined/location/project.properties</value>
<value>file:${AN_ENV_CONFIGURED_DIR}/project.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true" />
<property name="searchSystemEnvironment" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</bean>
This allowed us to have either a development (the first defaults) statically well known location, or a deployed location configured via env variables. The configurer processes these in order (i.e. the deployed takes precedence over the defaults).
这允许我们拥有一个静态众所周知的开发位置(第一个默认值),或者通过 env 变量配置的部署位置。配置器按顺序处理这些(即部署的优先于默认值)。
OLD
老的
I ended up going with a non programmatic approach. I used a MethodInvoker to retrieve the environment value. I was able to then pass that into the FileSystemResource.
我最终采用了非程序化的方法。我使用 MethodInvoker 来检索环境值。然后我能够将它传递到 FileSystemResource 中。
<bean id="configPath" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" >
<property name="targetClass" value="java.lang.String" />
<property name="staticMethod" value="java.lang.System.getenv" />
<property name="arguments">
<list>
<value>NAME_OF_VARIABLE</value>
</list>
</property>
</bean>
回答by Sanjay Ginde
You could always extend the FileSystemResource (i.e. PropertiesFileResource) that would initialize itself by taking prepending the property file location system property to the file path.
您始终可以扩展 FileSystemResource(即 PropertiesFileResource),通过将属性文件位置系统属性添加到文件路径来初始化自身。
回答by tobsen
In Spring.Net we have got the IVariableSourceinterface and PropertyPlaceholderConfigurerwhich are able to retrieve values from environment variables. Maybe there is something similar in the Spring Framework for Java?
在 Spring.Net 中,我们有IVariableSource接口和PropertyPlaceholderConfigurer,它们能够从环境变量中检索值。也许 Spring Framework for Java 中有类似的东西?
Edit:I thinkI found the corresponding java bean which is named PropertyPlaceholderConfigurer as well in the java docs.
编辑:我想我在 java docs 中也找到了名为 PropertyPlaceholderConfigurer 的相应 java bean 。
回答by Josh Long
while I note that the question required 2.5.x, it's worth pointing out that Spring 3's EL support (available since Nov 2009) would've made short work of this sort of thing
虽然我注意到这个问题需要 2.5.x,但值得指出的是 Spring 3 的 EL 支持(自 2009 年 11 月以来可用)会在这类事情上做一些简短的工作
@Value("#{ systemProperties['user.home'] }")
private String userHome ;
or
或者
<property name = "userHome" value ="#{ systemProperties['user.home'] }"/>
回答by Vladimir Nabokov
Example:
例子:
Start you app with -DDA_HOME=c:\temp
使用 -DDA_HOME=c:\temp 启动您的应用程序
c:\temp must contain directory called "config"
c:\temp 必须包含名为“config”的目录
In c:\temp\config you have file app.properties (for example)
在 c:\temp\config 你有文件 app.properties (例如)
Extend the Spring loader:
扩展 Spring 加载器:
public class Loader extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer{
private String appHome;
public void setAppHome(String appHome) {
this.appHome = appHome;
}
@Override
public void setLocation(Resource location) {
if(appHome==null){
throw new RuntimeException("You must specify VM property DA_HOME, this directory must contain " +
"another directory, called config, inside the config directory app.properties " +
"file must be found with the configuration properties");
}
String configurationDirectory = appHome + System.getProperty("file.separator") + "config";
String fileName = location.getFilename();
Resource file = new FileSystemResource( configurationDirectory + System.getProperty("file.separator")+ fileName);
super.setLocation(file);
}
}
Specify the new loader and its configuration base:
指定新的加载器及其配置基础:
<bean id="placeholderConfig" class="your.Loader">
<property name="appHome" value="#{systemProperties['DA_HOME']}"/>
<property name="location" value="app.properties" />
</bean>