SpringBoot 引用不同環境的配置文件, 放在外部的配置文件

SpringBoot 項目配置文件可以放在外部 
application-prod.properties
application-test.properties
application-dev.properties
這三種環境, 生產, 測試, 開發 環境的配置文件放在所在服務器的指定目錄下, 
application.properties 文件不用動, 還是放在項目裏.  用於配置 spring.profiles.active

需要對啓動類進行改造

@SpringBootApplication
public class SpringbootDemo1Application {
	
	public static void main(String[] args) throws IOException {
		SpringApplication app = new SpringApplication(SpringbootDemo1Application.class);
		Properties properties = new Properties();
		properties.load(new ClassPathResource("application.properties").getInputStream());
		properties.load(new FileInputStream(new File("/Users/xxxxx/application-"+ properties.getProperty("spring.profiles.active") +".properties")));
		app.setDefaultProperties(properties);
		app.run(args);
	}
}

 

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