Tomcat中用环境变量灵活定义spring数据库文件路径

来源:互联网 发布:医疗软件发展前景 编辑:程序博客网 时间:2024/05/18 22:41

相信很多公司都是基于window做JAVA开发,然后生产环境却是在Linux下,这样导致的一个问题就是JDBC配置文件路径不一致.
Spring项目中一般用PropertyPlaceholderConfigurer类在ApplicatonContex.xml中进行配置文件定义,如下:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="ignoreUnresolvablePlaceholders" value="true" />    <property name="locations">        <list>                <!--<value>classpath*:application.properties</value>-->                <value>file:/c:/jdbc.properties</value>        </list>    </property></bean> 

如果ApplicatinContext放到linux下怎么办?又得改成linux路径,这样实在太麻烦了.

这个时候就可以通过在tomcat中start文件中添加变量解决,编辑startup.sh(window下是startup.bat),在文件开始处添加变量如下:

export config_path='file:/data/tomcat/apache-vito/conf/mapmgr.properties'

ApplicatonContex.xml配置文件中PropertyPlaceholderConfigurer声明改为:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="ignoreUnresolvablePlaceholders" value="true" />    <property name="locations">        <list>            <!--<value>classpath*:application.properties</value>-->            <value>${config_path}</value>        </list>    </property></bean>

这样,每次启动tomcat,实例化spring容器时,PropertyPlaceholderConfigurer都会去读环境变量中的config_path,即tomcat的startup.sh中定义的路径值,屏蔽因系统路径不一致导致的繁琐操作。