tomcat啓動後修改項目配置文件

作用:啓動後修改spring文件配置,動態加載不同的配置文件


步驟:

1.修改catalina.sh文件,添加如下代碼在文件頭部:

JAVA_OPTS="-server -XX:PermSize=64M -XX:MaxPermSize=128M -Xmx2048m -Xms512m -Dfile.encoding=UTF-8 -Dmode=develop"


2.spring中加載屬性文件配置:

<!-- 屬性配置 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="locations">
		<list>
			<value>/WEB-INF/finance-webapp-${mode}.properties</value>
		</list>
	</property>
</bean>



3.屬性文件列表:

finance-webapp-develop.properties
finance-webapp-online.properties
finance-webapp-stage.properties
finance-webapp-test.properties

結果:系統啓動時,修改${mode}爲develop,從而加載finance-webapp-develop.properties文件配置


如果是idea開發工具,直接修改catalina.sh不會生效,需要在idea的tomcat配置VM options中添加'-Dmode=develop'


後續:

利用該配置控制定時器的執行:

只在生產環境執行代碼邏輯

if(!StringUtils.equals(System.getProperty("mode"), "online")){
	return;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章