我可以为Spring FileSystemResource使用基于环境变量的位置吗? [英] Can I use an Environment variable based location for Spring FileSystemResource?

查看:159
本文介绍了我可以为Spring FileSystemResource使用基于环境变量的位置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要求将所有属性文件存储在目录中。该目录的位置应存储在系统环境变量中。在我的应用程序上下文中,我将需要访问此环境变量来创建FileSystemResource bean。以下是我通常拥有的示例:

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:

<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>

相反,我需要让它像

<value>${prop_file_location}/myprops.properties</value>

其中prop文件位置是环境变量。有谁知道这样做的简单方法?

Where prop file location is an environment variable. Does anyone know an easy way of doing this?

我使用的是spring 2.5.6和java 1.6

I am using spring 2.5.6 and java 1.6

推荐答案

UPDATE



我们后来升级到Spring 3.0.X,我们可以利用spring表达式语言。我们的方法从三个bean简化为以下代码段:

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:

<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>

这使我们可以拥有一个开发(第一个默认值)静态知名位置,或部署通过env变量配置的位置。配置器按顺序处理这些(即部署优先于默认值)。

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).

我结束了采用非程序化方法。我使用MethodInvoker来检索环境值。然后我能够将其传递给FileSystemResource。

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.

<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>

这篇关于我可以为Spring FileSystemResource使用基于环境变量的位置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆