小编jin*_*xed的帖子

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

我有一个使用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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

<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" />
Run Code Online (Sandbox Code Playgroud)

java spring web-applications jvm-arguments

20
推荐指数
2
解决办法
4万
查看次数

标签 统计

java ×1

jvm-arguments ×1

spring ×1

web-applications ×1