ApplicationContextInitializer接口
用來初始化Spring ConfigurableApplicationContext應用上下文的回調接口,是在ConfigurableApplicationContext.refresh()以前調用。該接口典型的應用場景是web應用中須要編程方式對應用上下文作初始化。好比,註冊屬性源(property sources)或者針對上下文的環境信息environment激活相應的profilejava
代碼實現
默認的application.properties文件(默認指定爲生產環境)
test.name=defaultspring.profiles.active=prd
web
spring
application-dev.properties文件
test.name=dev
編程
application-prd.properties文件
test.name=prd
微信
自定義MyApplicationContextInitializer實現ApplicationContextInitializer接口,重寫initialize方法
package com.yxj.spring.Initializer;import com.yxj.spring.SpringBootTestRun;import org.springframework.context.ApplicationContextInitializer;import org.springframework.context.ConfigurableApplicationContext;/** * @ProjectName: springBootDemo * @Package: com.yxj.spring.Initializer * @Description: * @Author: 阿杰 * @CreateDate: 2019/1/25 22:39 * @UpdateUser: 暫無 * @UpdateDate: 2019/1/25 22:39 * @UpdateRemark: The modified content * @Version: 1.0 */public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { /** * 1.springBoot執行ApplicationContextInitializer refresh方法以前會先執行 * 2.在方法中設置活動配置文件爲dev(該結果能夠從其餘途徑去獲取,這裏給定默認值dev) * 3.在啓動類最後獲取name,查看最終結果 * @param applicationContext */ @Override public void initialize(ConfigurableApplicationContext applicationContext) { applicationContext.getEnvironment().setActiveProfiles("dev"); System.out.println("-------初始化---------"); }}
app
ide
svg
測試
flex
springBoot啓動類
package com.yxj.spring;import com.yxj.spring.Initializer.MyApplicationContextInitializer;import com.yxj.spring.monitor.MyApplicationListener;import com.yxj.spring.monitor.MyEvent;import com.yxj.spring.properties.TestProperties;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationContextInitializer;import org.springframework.context.ApplicationListener;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.annotation.Import;import java.util.List;/** * @ProjectName: springBootDemo * @Package: com.yxj.spring * @Description: * @Author: 阿杰 * @CreateDate: 2019/1/18 20:18 * @UpdateUser: 暫無 * @UpdateDate: 2019/1/18 20:18 * @UpdateRemark: The modified content * @Version: 1.0 */@SpringBootApplicationpublic class SpringBootTestRun { @Value("${test.name}") private String testName; /** * 添加初始化類方式 * 1.springBootTestRun.addInitializers(new MyApplicationContextInitializer()); * 2.經過配置項來指定(context.initiallzer.classes=com.yxj.spring.Initializer.MyApplicationContextInitializer) * 3.spring.factories機制 * 應用: * 當外部程序裝配這個模塊的時候,就能經過該jar包META-INF/services/裏的配置文件找到具體的實現類名,並裝載實例化,完成模塊的注入 * 在平常工做中,咱們可能須要實現一些SDK或者Spring Boot Starter給被人使用,這個使用咱們就可使用Factories機制。 * Factories機制可讓SDK或者Starter的使用只須要不多或者不須要進行配置,只須要在服務中引入咱們的jar包。 * 詳細使用參考:http://blog.51cto.com/13836814/2134630 * * @param args */ public static void main(String[] args) { SpringApplication springBootTestRun = new SpringApplication(SpringBootTestRun.class); //springBootTestRun.addInitializers(new MyApplicationContextInitializer()); ConfigurableApplicationContext run = springBootTestRun.run(args); SpringBootTestRun bean = run.getBean(SpringBootTestRun.class); System.out.println("------------分割線------------"); System.out.println(bean.testName); run.close(); }}
測試結果
-------初始化---------------------分割線------------dev
CommandLineRunner和ApplicationRunner接口
CommandLineRunner、ApplicationRunner 接口是在容器啓動成功後的最後一步回調。若是但願在項目啓動完成以前,初始化一些操做,可使用這兩個接口。
實現
實現CommandLineRunner或者ApplicationRunner接口
實現run方法
經過@Component或者其餘方式添加到spring容器管理中
能夠實現多個,經過@Order(value = XXX)的方式,能夠實現排序
CommandLineRunner接口
第一個CommandLineRunner
package com.yxj.spring.runner;import org.springframework.boot.CommandLineRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import java.util.Arrays;/** * @ProjectName: springBootDemo * @Package: com.yxj.spring.runner * @Description: * @Author: 阿杰 * @CreateDate: 2019/1/25 23:10 * @UpdateUser: 暫無 * @UpdateDate: 2019/1/25 23:10 * @UpdateRemark: The modified content * @Version: 1.0 */@Component@Order(value = 4)public class MyCommandLineRunner1 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("order value is 4 。MyCommandLineRunner1 run. get args : "+Arrays.asList(args)); }}
第二個CommandLineRunner
package com.yxj.spring.runner;import org.springframework.boot.CommandLineRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import java.util.Arrays;/** * @ProjectName: springBootDemo * @Package: com.yxj.spring.runner * @Description: * @Author: 阿杰 * @CreateDate: 2019/1/25 23:10 * @UpdateUser: 暫無 * @UpdateDate: 2019/1/25 23:10 * @UpdateRemark: The modified content * @Version: 1.0 */@Component@Order(value = 1)public class MyCommandLineRunner2 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("order value is 1 。MyCommandLineRunner2 run. get args : "+Arrays.asList(args)); }}
第一個ApplicationRunner
package com.yxj.spring.runner;import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;/** * @ProjectName: springBootDemo * @Package: com.yxj.spring.runner * @Description: * @Author: 阿杰 * @CreateDate: 2019/1/25 23:27 * @UpdateUser: 暫無 * @UpdateDate: 2019/1/25 23:27 * @UpdateRemark: The modified content * @Version: 1.0 */@Component@Order(value = 3)public class MyApplicationRunner1 implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { for(String argsString :args.getOptionNames()){ System.out.println("order value is 3 。MyApplicationRunner1 is ApplicationArguments key : ("+argsString + ") ApplicationArguments value : ("+args.getOptionValues(argsString)+")"); } }}
第二個ApplicationRunner
package com.yxj.spring.runner;import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;/** * @ProjectName: springBootDemo * @Package: com.yxj.spring.runner * @Description: * @Author: 阿杰 * @CreateDate: 2019/1/25 23:27 * @UpdateUser: 暫無 * @UpdateDate: 2019/1/25 23:27 * @UpdateRemark: The modified content * @Version: 1.0 */@Component@Order(value = 2)public class MyApplicationRunner2 implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { for(String argsString :args.getOptionNames()){ System.out.println("order value is 2 。MyApplicationRunner2 is ApplicationArguments key : ("+argsString + ") ApplicationArguments value : ("+args.getOptionValues(argsString)+")"); } }}
springBoot測試啓動類
package com.yxj.spring;import com.yxj.spring.Initializer.MyApplicationContextInitializer;import com.yxj.spring.monitor.MyApplicationListener;import com.yxj.spring.monitor.MyEvent;import com.yxj.spring.properties.TestProperties;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationContextInitializer;import org.springframework.context.ApplicationListener;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.annotation.Import;import java.util.List;/** * @ProjectName: springBootDemo * @Package: com.yxj.spring * @Description: * @Author: 阿杰 * @CreateDate: 2019/1/18 20:18 * @UpdateUser: 暫無 * @UpdateDate: 2019/1/18 20:18 * @UpdateRemark: The modified content * @Version: 1.0 */@SpringBootApplicationpublic class SpringBootTestRun { @Value("${test.name}") private String testName; public static void main(String[] args) { SpringApplication springBootTestRun = new SpringApplication(SpringBootTestRun.class); ConfigurableApplicationContext run = springBootTestRun.run("測試1","測試2","--key1=測試3","--key2=測試4"); run.close(); }}
測試結果
order value is 1 。MyCommandLineRunner2 run. get args : [測試1, 測試2, --key1=測試3, --key2=測試4]order value is 2 。MyApplicationRunner2 is ApplicationArguments key : (key1) ApplicationArguments value : ([測試3])order value is 2 。MyApplicationRunner2 is ApplicationArguments key : (key2) ApplicationArguments value : ([測試4])order value is 3 。MyApplicationRunner1 is ApplicationArguments key : (key1) ApplicationArguments value : ([測試3])order value is 3 。MyApplicationRunner1 is ApplicationArguments key : (key2) ApplicationArguments value : ([測試4])order value is 4 。MyCommandLineRunner1 run. get args : [測試1, 測試2, --key1=測試3, --key2=測試4]
對比
兩個都是在容器啓動成功後的最後一步回調
CommandLineRunner的參數是最原始的參數,沒有作任何處理。ApplicationRunner的參數是ApplicationArguments,是對原始參數作了進一步的封裝。
ApplicationRunner只對--key=value這種形式作解析,CommandLineRunner能夠得到傳遞的全部字符串
長按識別二維碼,瞭解更多
掃描二維碼加入技術交流羣!
本文分享自微信公衆號 - 亂敲代碼(lqcoder)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。