spring profiles 相似於maven profiles,不過spring profiles是在部署運行時選擇配置文件;而maven profiles是在打包時選擇所激活的配置文件,其它的文件就不包含進去了。兩種方式對於不一樣環境的部署提供了很大的方便:好比開發環境、測試環境、生產環境;不至於總是修改配置文件,如jdbc.properties,甚至有時還會忘記,就出大問題了。spring
如今src(即classpath路徑下)下有:jdbc_dev.properties/jdbc_test.properties/jdbc_production.properties三個文件;在開發環境、測試環境、生產環境分別配置環境變量:spring.profiles.active=dev、spring.profiles.active=test、spring.profiles.active=production,他們是一一對應的。tomcat
在spring的xml配置文件中,能夠用以下方式加載jdbc屬性文件maven
<context:property-placeholder location="classpath:jdbc_${spring.profiles.active}.properties"/>測試
經過設置環境變量,這是推薦的作法。還有一種方式就是在tomcat中配置:spa
在%TOMCAT_HOME%/conf/catalina.properties文件中加一個屬性配置,好比開發環境就是spring.profiles.active=devxml
總結:經過如上配置,就會使得spring在相應的環境讀取相應的配置文件。
開發