Spring 数据库连接池读取系统环境变量作为参数

原来是写在一个properties文件里面,后来项目要部署的的确太多了,每次更改不太方便,就想把这些固定不变的信息写在当地的环境变量里面

原先是这样的:引用的所有信息在jdbc.properties

<bean id="propertyConfigurer"
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>
 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
</bean>

更改后是利用 org.springframework.context.support.PropertySourcesPlaceholderConfigurer

此类实现了EnvironmentAware 接口,可以实现读取系统环境变量

linux修改 /etc/profile  

新增 export jdbc_url = 你的url

   export jdbc_usrname = 你的usrname

<bean id="propertyConfigurer"
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>
 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="url" value="${jdbc_url}"/>
    <property name="username" value="${jdbc_username}"/>
</bean>

这样就可以读取系统环境变量了~~~~

原文地址:https://www.cnblogs.com/qiaoyihang/p/6674016.html