如何在Spring applicationContext.xml中读取JVM参数 [英] How do I read JVM arguments in the Spring applicationContext.xml

查看:585
本文介绍了如何在Spring applicationContext.xml中读取JVM参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

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?

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

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.

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

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


推荐答案

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

You can use Spring EL expressions, then it is #{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!

$ 会引用占位符,而引用bean, systemProperties 是bean。

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

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

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


-Denv = development ,例如...

但在弹簧配置中,您将其命名为 environment 。但两者当然必须相等!

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

这篇关于如何在Spring applicationContext.xml中读取JVM参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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