如何在Spring applicationContext.xml中读取JVM参数

时间:2021-11-17 14:08:34

I have a JSF web application with Spring and I am trying to figure out a way to reference the JVM arguments from the applicationContext.xml. I am starting the JVM with an environment argument (-Denv=development, for example). I have found and tried a few different approaches including:

我有一个使用Spring的JSF Web应用程序,我正在尝试找出一种从applicationContext.xml引用JVM参数的方法。我使用环境参数启动JVM(例如-Denv = development)。我找到并尝试了一些不同的方法,包括:

<bean id="myBean" class="com.foo.bar.myClass">
  <property name="environment">
    <value>${environment}</value>
  </property>
</bean>

But, when the setter method is invoked in MyClass, the string "${environment}" is passed, instead of "development". I have a work around in place to use System.getProperty(), but it would be nicer, and cleaner, to be able to set these values via Spring. Is there any way to do this?

但是,当在MyClass中调用setter方法时,将传递字符串“$ {environment}”,而不是“development”。我有一个使用System.getProperty()的工作,但是能够通过Spring设置这些值会更好,更清晰。有没有办法做到这一点?

Edit: What I should have mentioned before is that I am loading properties from my database using a JDBC connection. This seems to add complexity, because when I add a property placeholder to my configuration, the properties loaded from the database are overridden by the property placeholder. I'm not sure if it's order-dependent or something. It's like I can do one or the other, but not both.

编辑:我之前应该提到的是我使用JDBC连接从我的数据库加载属性。这似乎增加了复杂性,因为当我向配置添加属性占位符时,属性占位符将覆盖从数据库加载的属性。我不确定它是依赖于顺序还是其他东西。这就像我可以做其中一个,但不是两个。

Edit: I'm currently loading the properties using the following configuration:

编辑:我目前正在使用以下配置加载属性:

<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc.mydb.myschema"/> 
</bean>

<bean id="props" class="com.foo.bar.JdbcPropertiesFactoryBean">
    <property name="jdbcTemplate">
        <bean class="org.springframework.jdbc.core.JdbcTemplate">
            <constructor-arg ref="myDataSource" />
        </bean>
    </property>
</bean>

<context:property-placeholder properties-ref="props" />

6 个解决方案

#1


27  

You can use Spring EL expressions, then it is #{systemProperties.test} for -Dtest="hallo welt"

你可以使用Spring EL表达式,然后它是#{systemProperties.test} for -Dtest =“hallo welt”

In your case it should be:

在你的情况下它应该是:

<bean id="myBean" class="com.foo.bar.myClass">
  <property name="environment">
    <value>#{systemProperties.environment}</value>
  </property>
</bean>

The # instead of $ is no mistake!

#而不是$是没错的!

$ would refer to place holders, while # refers to beans, and systemProperties is a bean.

$表示占位符,而#表示bean,而systemProperties是bean。


May it is only a spelling error, but may it is the cause for your problem: In the example for your command line statement you name the variable env

可能只是拼写错误,但可能是您的问题的原因:在命令行语句的示例中,您将变量命名为env

(-Denv=development, for example...

(-Denv =开发,例如...

But in the spring configuration you name it environment. But both must be equals of course!

但在弹簧配置中,您将其命名为环境。但两者当然必须是平等的!

#2


10  

If you register a PropertyPlaceholderConfigurer it will use system properties as a fallback.

如果您注册PropertyPlaceholderConfigurer,它将使用系统属性作为后备。

For example, add

例如,添加

<context:property-placeholder/>

to your configuration. Then you can use ${environment} in either your XML configuration or in @Value annotations.

到你的配置。然后,您可以在XML配置或@Value注释中使用$ {environment}。

#3


3  

You can load a property file based on system property env like this:

您可以根据系统属性env加载属性文件,如下所示:

   <bean id="applicationProperties"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="ignoreResourceNotFound" value="false" />
      <property name="ignoreUnresolvablePlaceholders" value="true" />
      <property name="searchSystemEnvironment" value="false" />
      <property name="locations">
         <list>
            <value>classpath:myapp-${env:prod}.properties</value>
         </list>
      </property>
   </bean>

If env is not set default it to production otherwise development and testing teams can have their flavor of app by setting -Denv=development or -Denv=testing accordingly.

如果未将env设置为默认生产,否则开发和测试团队可以通过设置-Denv = development或-Denv =相应的测试来获得应用程序的风格。

#4


1  

Use #{systemProperties['env']} Basically pass the propertyName used in Java command line as -DpropertyName=value. In this case it was -Denv=development so used env.

使用#{systemProperties ['env']}基本上将Java命令行中使用的propertyName作为-DpropertyName = value传递。在这种情况下,它是-Denv =开发所以使用环境。

#5


0  

Interestingly, Spring has evolved to handled this need more gracefully with PropertySources: http://spring.io/blog/2011/02/15/spring-3-1-m1-unified-property-management/

有趣的是,Spring已经发展到使用PropertySources更优雅地处理这种需求:http://spring.io/blog/2011/02/15/spring-3-1-m1-unified-property-management/

With a few configurations and perhaps a custom ApplicationInitializer if you are working on a Web app, you can have the property placeholder handle System, Environment, and custom properties. Spring provides PropertySourcesPlaceholderConfigurer which is used when you have in your Spring config. That one will look for properties in your properties files, then System, and then finally Environment.

如果您正在使用Web应用程序,可以使用一些配置和自定义ApplicationInitializer,您可以使用属性占位符处理系统,环境和自定义属性。 Spring提供了PropertySourcesPlaceholderConfigurer,它在你的Spring配置中使用。那个会在你的属性文件中寻找属性,然后是System,然后是环境。

#6


0  

Spring 3.0.7

Spring 3.0.7

<context:property-placeholder location="classpath:${env:config-prd.properties}" />

And at runtime set: -Denv=config-dev.properties

并在运行时设置:-Denv = config-dev.properties

If not set "env" will use default "config-prd.properties".

如果没有设置“env”将使用默认的“config-prd.properties”。

#1


27  

You can use Spring EL expressions, then it is #{systemProperties.test} for -Dtest="hallo welt"

你可以使用Spring EL表达式,然后它是#{systemProperties.test} for -Dtest =“hallo welt”

In your case it should be:

在你的情况下它应该是:

<bean id="myBean" class="com.foo.bar.myClass">
  <property name="environment">
    <value>#{systemProperties.environment}</value>
  </property>
</bean>

The # instead of $ is no mistake!

#而不是$是没错的!

$ would refer to place holders, while # refers to beans, and systemProperties is a bean.

$表示占位符,而#表示bean,而systemProperties是bean。


May it is only a spelling error, but may it is the cause for your problem: In the example for your command line statement you name the variable env

可能只是拼写错误,但可能是您的问题的原因:在命令行语句的示例中,您将变量命名为env

(-Denv=development, for example...

(-Denv =开发,例如...

But in the spring configuration you name it environment. But both must be equals of course!

但在弹簧配置中,您将其命名为环境。但两者当然必须是平等的!

#2


10  

If you register a PropertyPlaceholderConfigurer it will use system properties as a fallback.

如果您注册PropertyPlaceholderConfigurer,它将使用系统属性作为后备。

For example, add

例如,添加

<context:property-placeholder/>

to your configuration. Then you can use ${environment} in either your XML configuration or in @Value annotations.

到你的配置。然后,您可以在XML配置或@Value注释中使用$ {environment}。

#3


3  

You can load a property file based on system property env like this:

您可以根据系统属性env加载属性文件,如下所示:

   <bean id="applicationProperties"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="ignoreResourceNotFound" value="false" />
      <property name="ignoreUnresolvablePlaceholders" value="true" />
      <property name="searchSystemEnvironment" value="false" />
      <property name="locations">
         <list>
            <value>classpath:myapp-${env:prod}.properties</value>
         </list>
      </property>
   </bean>

If env is not set default it to production otherwise development and testing teams can have their flavor of app by setting -Denv=development or -Denv=testing accordingly.

如果未将env设置为默认生产,否则开发和测试团队可以通过设置-Denv = development或-Denv =相应的测试来获得应用程序的风格。

#4


1  

Use #{systemProperties['env']} Basically pass the propertyName used in Java command line as -DpropertyName=value. In this case it was -Denv=development so used env.

使用#{systemProperties ['env']}基本上将Java命令行中使用的propertyName作为-DpropertyName = value传递。在这种情况下,它是-Denv =开发所以使用环境。

#5


0  

Interestingly, Spring has evolved to handled this need more gracefully with PropertySources: http://spring.io/blog/2011/02/15/spring-3-1-m1-unified-property-management/

有趣的是,Spring已经发展到使用PropertySources更优雅地处理这种需求:http://spring.io/blog/2011/02/15/spring-3-1-m1-unified-property-management/

With a few configurations and perhaps a custom ApplicationInitializer if you are working on a Web app, you can have the property placeholder handle System, Environment, and custom properties. Spring provides PropertySourcesPlaceholderConfigurer which is used when you have in your Spring config. That one will look for properties in your properties files, then System, and then finally Environment.

如果您正在使用Web应用程序,可以使用一些配置和自定义ApplicationInitializer,您可以使用属性占位符处理系统,环境和自定义属性。 Spring提供了PropertySourcesPlaceholderConfigurer,它在你的Spring配置中使用。那个会在你的属性文件中寻找属性,然后是System,然后是环境。

#6


0  

Spring 3.0.7

Spring 3.0.7

<context:property-placeholder location="classpath:${env:config-prd.properties}" />

And at runtime set: -Denv=config-dev.properties

并在运行时设置:-Denv = config-dev.properties

If not set "env" will use default "config-prd.properties".

如果没有设置“env”将使用默认的“config-prd.properties”。