如何在Spring xml配置中注入环境变量? [英] How to inject environmental variables inside spring xml configuration?

查看:1531
本文介绍了如何在Spring xml配置中注入环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AWS在 System.getProperty(JDBC_CONNECTION_STRING) /create_deploy_Java.managing.htmlrel =nofollow noreferrer> http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.managing.html ,我们设置了我们的环境变量。除了我不能在我的Spring XML配置代码内调用 System.getProperty 以外,我可以调用资源束快捷方式,因为资源包本身必须将这些环境变量提取到服务他们可以请帮我请转换这个示例配置来使用环境变量吗? : - )

 < bean id =dataSourceclass =org.apache.commons .dbcp.BasicDataSource> 
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// secrethost:007 / whois?autoReconnect = true/>
< property name =usernamevalue =bond/>
< property name =passwordvalue =abuginsidemistycorner/>
< property name =initialSizevalue =100/>

< property name =minEvictableIdleTimeMillis>
< value> 300000< / value>
< / property>

< property name =timeBetweenEvictionRunsMillis>
< value> 60000< / value>
< / property>

< property name =maxIdlevalue =20/>
< / bean>

我无法理解人们在这里做什么:



我可以为Spring FileSystemResource使用一个基于Environment变量的位置吗?这将适用于最近的春季版本?

在您的配置中添加一个< context:property-placeholder .. /> 元素。

 < context:property-placeholder /> 

然后只需在配置中使用占位符。



< class class =org.apache.commons.dbcp.BasicDataSource>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =$ {JDBC_CONNECTION_STRING}/>
< property name =usernamevalue =bond/>
< property name =passwordvalue =abuginsidemistycorner/>
< property name =initialSizevalue =100/>
< property name =minEvictableIdleTimeMillisvalue =30000/>
< property name =timeBetweenEvictionRunsMillisvalue =60000/>
< property name =maxIdlevalue =20/>
< / bean>

确保占位符名称与您设置的变量相匹配。


AWS talks about System.getProperty("JDBC_CONNECTION_STRING") in http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.managing.html after we set up our environmental variables. All great except I can't call System.getProperty inside my Spring XML configuration code nor I can call for resource bundle shortcuts since resources bundle itself has to extract somehow these environmental variables to serve them. Could you kindly help me please to convert this example config to use environmental variables ? :-)

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://secrethost:007/whois?autoReconnect=true" />
    <property name="username" value="bond" />
    <property name="password" value="abuginsidemistycorner" />
    <property name="initialSize" value="100" />

    <property name="minEvictableIdleTimeMillis">
        <value>300000</value>
    </property>

    <property name="timeBetweenEvictionRunsMillis">
        <value>60000</value>
    </property>

    <property name="maxIdle" value="20" />
</bean>

I was not able to understand what do people do here:

Can I use an Environment variable based location for Spring FileSystemResource? which would work for recent spring version?

解决方案

First add a <context:property-placeholder .. /> element to your configuration.

<context:property-placeholder />

Then simply use placeholders in your config.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="${JDBC_CONNECTION_STRING}" />
    <property name="username" value="bond" />
    <property name="password" value="abuginsidemistycorner" />
    <property name="initialSize" value="100" />
    <property name="minEvictableIdleTimeMillis" value="30000" />
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <property name="maxIdle" value="20" />
</bean>

Make sure that the placeholder names match your variables you have setup.

这篇关于如何在Spring xml配置中注入环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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