動(dòng)態(tài)管理配置文件擴(kuò)展接口EnvironmentPostProcessor

SpringBoot支持動(dòng)態(tài)的讀取文件,留下的擴(kuò)展接口org.springframework.boot.env.EnvironmentPostProcessor。這個(gè)接口是spring包下的,使用這個(gè)進(jìn)行配置文件的集中管理,而不需要每個(gè)項(xiàng)目都去配置配置文件。這種方法也是springboot框架留下的一個(gè)擴(kuò)展(可以自己去擴(kuò)展)

demo

/Users/naeshihiroshi/study/studySummarize/SpringBoot/(自己測試也可以隨機(jī)在一個(gè)目錄下建立一文件),目錄下建立一個(gè)名為springboot.properties文件,

springboot.properties中定義一些配置,配置如下:

jdbc.root.user=zhihao.miao
jdbc.root.password=242312321

定義MyEnvironmentPostProcessor實(shí)現(xiàn)EnvironmentPostProcessor接口

@Component
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        try{
            InputStream inputStream = new FileInputStream("/Users/naeshihiroshi/study/studySummarize/SpringBoot/springboot.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("my",properties);
            environment.getPropertySources().addLast(propertiesPropertySource);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

在classpath定義一個(gè)META-INF文件夾然后在其下面先建spring.factories文件,在其中指定:

org.springframework.boot.env.EnvironmentPostProcessor=com.zhihao.miao.processor.MyEnvironmentPostProcessor‘

啟動(dòng)類測試:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class,args);
        String username = context.getEnvironment().getProperty("jdbc.root.user");
        String password = context.getEnvironment().getProperty("jdbc.root.password");
        System.out.println("username==="+username);
        System.out.println("password==="+password);
        context.close();
    }
}

打印結(jié)果:

username===zhihao.miao
password===242312321

EnvironmentPostProcessor接口官網(wǎng)說明

官方文檔如下

Allows for customization of the application's {@link Environment} prior to the application context being refreshed.
允許定制應(yīng)用的上下文的應(yīng)用環(huán)境優(yōu)于應(yīng)用的上下文之前被刷新。(意思就是在spring上下文構(gòu)建之前可以設(shè)置一些系統(tǒng)配置)

EnvironmentPostProcessor implementations have to be registered in
META-INF/spring.factories, using the fully qualified name of this class as the key.
EnvironmentPostProcessor的實(shí)現(xiàn)類必須要在META-INF/spring.factories文件中去注冊,并且注冊的是全類名。

EnvironmentPostProcessor processors are encouraged to detect whether Spring's
org.springframework.core.Ordered Ordered interface has been implemented or if the @org.springframework.core.annotation.Order Order annotation is present and to sort instances accordingly if so prior to invocation.
鼓勵(lì)EnvironmentPostProcessor處理器檢測Org.springframework.core.Ordered注解,這樣相應(yīng)的實(shí)例也會(huì)按照@Order注解的順序去被調(diào)用。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容