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

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

问题描述

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

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}",而不是development".我有一个使用 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"

在你的情况下应该是:

<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,例如...

但是在 spring 配置中,您将其命名为 environment.但两者当然必须相等!

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

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

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