SpringBoot 打包爲war包啓動時導入外部配置文件

最近在做一個SpirngBoot的項目,要求服務器部署的時候使用tomcat啓動war包的時候需要導入一個指定位置的application.properties文件。在網上查找了相關的問題之後,發現大部分都是基於jar包的,同樣的方法war包下並不適用。
後來發現了一個方法,可以爲完美解決這個問題。
這裏寫圖片描述
在你的項目文件夾下,創建一個configuration文件夾用於存儲各種SpringBoot的配置文件,然後新建一個Java類LocalSettingsEnvironmentPostProcessor。

package com.altynai.xxxxxx.configuration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

public class LocalSettingsEnvironmentPostProcessor implements EnvironmentPostProcessor{
    private static final String LOCATION = "C:\\xxxx\\application.properties";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
        File file = new File(LOCATION);
//        File file = new File(System.getProperty("user.home"), LOCATION);
//        System.out.println("user.home" + System.getProperty("user.home"));
        if (file.exists()) {
            MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
//            System.out.println("Loading local settings from " + file.getAbsolutePath());
            Properties properties = loadProperties(file);
//            System.out.println(properties.toString());
            propertySources.addFirst(new PropertiesPropertySource("Config", properties));
        }
    }

    private Properties loadProperties(File f) {
        FileSystemResource resource = new FileSystemResource(f);
        try {
            return PropertiesLoaderUtils.loadProperties(resource);
        }
        catch (IOException ex) {
            throw new IllegalStateException("Failed to load local settings from " + f.getAbsolutePath(), ex);
        }
    }
}

這個java類的作用就是根據指定的配置文件路徑,讀取文件,添加到程序運行的環境中。注意,根據我的測試,這裏導入的外部配置文件只能是.properties的文件,如果是.yml的話會有問題。
然後在你的resources文件夾下創建一個文件夾名爲META-INF,在裏面創建一個spring.factories的文件,文件內容如下:

org.springframework.boot.env.EnvironmentPostProcessor=com.altynai.xxxxx.configuration.LocalSettingsEnvironmentPostProcessor

這個文件的作用就是設置SpringBoot服務啓動的時候調用我們剛纔寫的那個Java類。

至此,你的war包在使用tomcat啓動的時候就應該可以讀取制定位置的外部文件了。我的文件結構如下,僅供參考
這裏寫圖片描述
這裏還需要說明的是假設你原本的配置文件的設置屬性與導入了外部配置文件的設置屬性重複了最終系統運行起來使用的是哪一個?
答案是,看兩個配置文件加入程序環境的先後順序,後面的會覆蓋前面的。
上面的Java類中有這個一段代碼,這裏的意思就是,外部的文件是最先導入的。

 propertySources.addFirst(new PropertiesPropertySource("Config", properties));

如果設置爲下面的這個

 propertySources.addLast(new PropertiesPropertySource("Config", properties));

則表示最後導入外部配置文件。

參考鏈接:
[1] https://www.youtube.com/watch?v=uof5h-j0IeE&feature=youtu.be&t=1h17m46s
[2] https://www.youtube.com/watch?v=uof5h-j0IeE&feature=youtu.be&t=1h17m46s

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