如何在Spring applicationContext中读取系统环境变量

jai*_*jai 108 java spring environment-variables

如何在应用程序上下文中读取系统环境变量?

我想要的东西:

<util:properties id="dbProperties"
        location="classpath:config_DEV/db.properties" />
Run Code Online (Sandbox Code Playgroud)

要么

<util:properties id="dbProperties"
        location="classpath:config_QA/db.properties" />
Run Code Online (Sandbox Code Playgroud)

取决于环境.

我的应用程序上下文中可以有这样的东西吗?

<util:properties id="dbProperties"
        location="classpath:config_${systemProperties.env}/db.properties" />
Run Code Online (Sandbox Code Playgroud)

其中实际的val是根据SYSTEM ENVIRONMENT VARIABLE设置的

我正在使用Spring 3.0

amr*_*mra 102

你很接近:o)Spring 3.0增加了Spring Expression Language.您可以使用

<util:properties id="dbProperties" 
    location="classpath:config_#{systemProperties['env']}/db.properties" />
Run Code Online (Sandbox Code Playgroud)

结合java ... -Denv=QA应该解决你的问题.

另请注意@yiling的评论:

为了访问系统环境变量,即操作系统级变量,如amoe评论,我们可以简单地在该EL中使用"systemEnvironment"而不是"systemProperties".喜欢 #{systemEnvironment['ENV_VARIABLE_NAME']}

  • 为了访问系统环境变量,即操作系统级变量,如amoe评论,我们可以简单地在该EL中使用"systemEnvironment"而不是"systemProperties".比如`#{systemEnvironment ['ENV_VARIABLE_NAME']}`. (19认同)
  • 您设置了java系统属性值.您可以在代码中读取此值,例如`assert System.getProperty("env")=="QA";` (2认同)
  • -Dprop = ...在命令行中设置java属性.您可以通过`System.getProperty("prop")`读取此属性.如果您想读取OS属性,请使用`System.getenv("os-env-variable")`.请参阅javadoc:http://docs.oracle.com/javase/6/docs/api/java/lang/System.html (2认同)

Boz*_*zho 49

看看这篇文章.它为您提供了几种方法,通过PropertyPlaceholderConfigurer它支持外部属性(通过systemPropertiesMode属性).


Dar*_*usz 46

现在你可以放

@Autowired
private Environment environment;
Run Code Online (Sandbox Code Playgroud)

在您的@Component,@Bean等等,然后通过Environment类访问属性:

environment.getProperty("myProp");
Run Code Online (Sandbox Code Playgroud)

对于单一属性@Bean

@Value("${my.another.property:123}") // value after ':' is the default
Integer property;
Run Code Online (Sandbox Code Playgroud)

另一种方式是方便的@ConfigurationProperties豆类:

@ConfigurationProperties(prefix="my.properties.prefix")
public class MyProperties {
  // value from my.properties.prefix.myProperty will be bound to this variable
  String myProperty;

  // and this will even throw a startup exception if the property is not found
  @javax.validation.constraints.NotNull
  String myRequiredProperty;

  //getters
}

@Component
public class MyOtherBean {
  @Autowired
  MyProperties myProperties;
}
Run Code Online (Sandbox Code Playgroud)

注意:请记住在设置新环境变量后重新启动eclipse


Ist*_*tao 25

是的,你可以这样做<property name="defaultLocale" value="#{ systemProperties['user.region']}"/>.

变量systemProperties是预定义的,请参见6.4.1基于XML的配置.


Bra*_*rks 8

在bean定义中,确保包含"searchSystemEnvironment"并将其设置为"true".如果您使用它来构建文件的路径,请将其指定为file:/// url.

例如,如果您有一个配置文件

/testapp/config/my.app.config.properties
Run Code Online (Sandbox Code Playgroud)

然后像这样设置一个环境变量:

MY_ENV_VAR_PATH=/testapp/config
Run Code Online (Sandbox Code Playgroud)

并且您的应用可以使用如下bean定义加载文件:

例如

<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="searchSystemEnvironment" value="true" />
    <property name="searchContextAttributes" value="true" />
    <property name="contextOverride" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>file:///${MY_ENV_VAR_PATH}/my.app.config.properties</value>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

  • 现在如何在java中读取加载的属性? (2认同)

小智 7

使用Spring EL,您可以按如下方式编写示例

<bean id="myBean" class="path.to.my.BeanClass">
    <!-- can be overridden with -Dtest.target.host=http://whatever.com -->
    <constructor-arg value="#{systemProperties['test.target.host'] ?: 'http://localhost:18888'}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)


eis*_*eis 5

对于我的用例,我只需要访问系统属性,但是在未定义的情况下提供默认值.

这是你如何做到的:

<bean id="propertyPlaceholderConfigurer"   
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="searchSystemEnvironment" value="true" />
</bean>  
<bean id="myBean" class="path.to.my.BeanClass">
    <!-- can be overridden with -Dtest.target.host=http://whatever.com -->
    <constructor-arg value="${test.target.host:http://localhost:18888}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)