最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot ApplicationContextInitializer的三種使用方法小結(jié)

 更新時(shí)間:2021年11月17日 15:50:52   作者:leileibest_437147623  
這篇文章主要介紹了關(guān)于ApplicationContextInitializer的三種使用方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

ApplicationContextInitializer的三種使用方法

概述

ApplicationContextInitializer是在springboot啟動(dòng)過程(refresh方法前)調(diào)用,主要是在ApplicationContextInitializer中initialize方法中拉起了ConfigurationClassPostProcessor這個(gè)類(我在springboot啟動(dòng)流程中有描述),通過這個(gè)processor實(shí)現(xiàn)了beandefinition。

言歸正傳,ApplicationContextInitializer實(shí)現(xiàn)主要有3中方式:

1、使用spring.factories方式

首先我們自定義個(gè)類實(shí)現(xiàn)了ApplicationContextInitializer,然后在resource下面新建/META-INF/spring.factories文件。

 
public class Demo01ApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        System.out.println("user add method ==> ApplicationContextInitializer");
    }
}

這個(gè)加載過程是在SpringApplication中的getSpringFactoriesInstances()方法中直接加載并實(shí)例后執(zhí)行對(duì)應(yīng)的initialize方法。代碼如下:

private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type,
			Class<?>[] parameterTypes, Object... args) {
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		// Use names and ensure unique to protect against duplicates
		Set<String> names = new LinkedHashSet<String>(
				SpringFactoriesLoader.loadFactoryNames(type, classLoader));
		List<T> instances = createSpringFactoriesInstances(type, parameterTypes,
				classLoader, args, names);
		AnnotationAwareOrderComparator.sort(instances);
		return instances;
	}

2、application.properties添加配置方式

對(duì)于這種方式是通過DelegatingApplicationContextInitializer這個(gè)初始化類中的initialize方法獲取到application.properties中context.initializer.classes對(duì)應(yīng)的類并執(zhí)行對(duì)應(yīng)的initialize方法。

只需要將實(shí)現(xiàn)了ApplicationContextInitializer的類添加到application.properties即可。如下:

下面我們看看DelegatingApplicationContextInitializer是如何加載的??创a:

private static final String PROPERTY_NAME = "context.initializer.classes";
private List<Class<?>> getInitializerClasses(ConfigurableEnvironment env) {
		String classNames = env.getProperty(PROPERTY_NAME);
		List<Class<?>> classes = new ArrayList<Class<?>>();
		if (StringUtils.hasLength(classNames)) {
			for (String className : StringUtils.tokenizeToStringArray(classNames, ",")) {
				classes.add(getInitializerClass(className));
			}
		}
		return classes;
	}

是從配置文件中獲取到對(duì)應(yīng)的初始化類信息,然后執(zhí)行初始化方法。

3、直接通過add方法

這個(gè)方法就比較簡單,直接在springboot啟動(dòng)的時(shí)候,add一個(gè)實(shí)現(xiàn)了ApplicationContextInitializer的類即可,代碼如下:

@SpringBootApplication
public class InitializerDemoApplication { 
	public static void main(String[] args) {
		//type01
		SpringApplication springApplication = new SpringApplication(InitializerDemoApplication.class);
		springApplication.addInitializers(new Demo01ApplicationContextInitializer());
		springApplication.run(args);
 
		//SpringApplication.run(InitializerDemoApplication.class,args);
	}
}

以上3中方法都可以實(shí)現(xiàn)自定義的Initializer,只不過執(zhí)行的順序有差異。這里我比較感興趣有2個(gè),一個(gè)通過spring.factories實(shí)現(xiàn)SPI模式,有興趣的可以看下jdbc-starter等一些相關(guān)springboot starter。

第二個(gè)就是作為一個(gè)鉤子去拉起來"一坨"的bean。

ApplicationContextInitializer都干了些什么

初始化方法

org.springframework.boot.context.config.DelegatingApplicationContextInitializer

 @Override
 public void initialize(ConfigurableApplicationContext context) {
  ConfigurableEnvironment environment = context.getEnvironment();
  /**
  * 初始化環(huán)境變量中的context.initializer.classes指定的類
  **/
  List<Class<?>> initializerClasses = getInitializerClasses(environment);
  if (!initializerClasses.isEmpty()) {
   applyInitializerClasses(context, initializerClasses);
  }
 }

也就是說沒有定義的話,就不會(huì)初始化了。

org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer

 @Override
 public void initialize(ConfigurableApplicationContext applicationContext) {
  /**
  * 注冊(cè)一個(gè)元數(shù)據(jù)讀取的工廠類
  **/
  applicationContext.addBeanFactoryPostProcessor(new CachingMetadataReaderFactoryPostProcessor());
 }

org.springframework.boot.context.ContextIdApplicationContextInitializer

 @Override
 public void initialize(ConfigurableApplicationContext applicationContext) {
  ContextId contextId = getContextId(applicationContext);
  applicationContext.setId(contextId.getId());
  /**
  * 注冊(cè)一個(gè)ContextId對(duì)象
  **/
  applicationContext.getBeanFactory().registerSingleton(ContextId.class.getName(), contextId);
 }

org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer

 @Override
 public void initialize(ConfigurableApplicationContext context) {
  /**
  * 注入ComponentScan檢查處理對(duì)象
  **/
  context.addBeanFactoryPostProcessor(new ConfigurationWarningsPostProcessor(getChecks()));
 }

org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer

 @Override
 public void initialize(ConfigurableApplicationContext applicationContext) {
   /**
  * 注入一個(gè)端口檢查和設(shè)置的監(jiān)聽器,對(duì)應(yīng)的事件RSocketServerInitializedEvent
  **/
  applicationContext.addApplicationListener(new Listener(applicationContext));
 }

org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer

 @Override
 public void initialize(ConfigurableApplicationContext applicationContext) {
  /**
   注冊(cè)了一個(gè)監(jiān)聽org.springframework.boot.web.context.WebServerInitializedEvent事件的監(jiān)聽器,用于設(shè)置端口信息
  **/
  applicationContext.addApplicationListener(this);
 }

org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener

 @Override
 public void initialize(ConfigurableApplicationContext applicationContext) {
  this.applicationContext = applicationContext;
  applicationContext.addApplicationListener(new ConditionEvaluationReportListener());
  if (applicationContext instanceof GenericApplicationContext) {
   // Get the report early in case the context fails to load
   // 注冊(cè)一個(gè)監(jiān)聽ApplicationEvent事件的監(jiān)聽器用于打印自動(dòng)配置后的日志信息
   this.report = ConditionEvaluationReport.get(this.applicationContext.getBeanFactory());
  }
 }

所有的這些初始化類都沒偶進(jìn)行啟動(dòng)服務(wù)的實(shí)質(zhì)性操作,都是通過注冊(cè)對(duì)象,埋點(diǎn),后面invokeBeanFactoryPostProcessors才真正調(diào)用初始化方法,而且在項(xiàng)目啟動(dòng)之前

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用hibernate和struts2實(shí)現(xiàn)分頁功能的示例

    使用hibernate和struts2實(shí)現(xiàn)分頁功能的示例

    本篇文章主要介紹了使用hibernate和struts2實(shí)現(xiàn)分頁功能,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • Java 線程池詳解

    Java 線程池詳解

    本文給大家總結(jié)了java中的線程池的相關(guān)問題,非常的詳細(xì)也很實(shí)用,有需要的小伙伴可以參考下。
    2016-03-03
  • JPA-JpaRepository方法命名語法說明

    JPA-JpaRepository方法命名語法說明

    這篇文章主要介紹了JPA-JpaRepository方法命名語法說明,具有很好的參考價(jià)值。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringBoot集成內(nèi)存數(shù)據(jù)庫H2的實(shí)踐

    SpringBoot集成內(nèi)存數(shù)據(jù)庫H2的實(shí)踐

    h2是內(nèi)存數(shù)據(jù)庫,查詢高效,可以在開發(fā)初期使用它。本文主要介紹了SpringBoot集成內(nèi)存數(shù)據(jù)庫H2的實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-09-09
  • java、freemarker保留兩位小數(shù)

    java、freemarker保留兩位小數(shù)

    這篇文章主要介紹了 java、freemarker保留兩位小數(shù)的實(shí)現(xiàn)方法,然后給大家補(bǔ)充介紹了freemarker保留兩位小數(shù)的詳解,需要的朋友可以參考下
    2017-03-03
  • Spring事務(wù)失效場景原理及解決方案

    Spring事務(wù)失效場景原理及解決方案

    這篇文章主要介紹了Spring事務(wù)失效場景原理及解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot項(xiàng)目中使用Swagger2及注解解釋的詳細(xì)教程

    SpringBoot項(xiàng)目中使用Swagger2及注解解釋的詳細(xì)教程

    Swagger2是一個(gè)開源項(xiàng)目,用于為RESTful Web服務(wù)生成REST API文檔,下面這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目中使用Swagger2及注解解釋的詳細(xì)教程,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • 詳解Java數(shù)據(jù)庫連接JDBC基礎(chǔ)知識(shí)(操作數(shù)據(jù)庫:增刪改查)

    詳解Java數(shù)據(jù)庫連接JDBC基礎(chǔ)知識(shí)(操作數(shù)據(jù)庫:增刪改查)

    這篇文章主要介紹了詳解Java數(shù)據(jù)庫連接JDBC基礎(chǔ)知識(shí)(操作數(shù)據(jù)庫:增刪改查),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • 詳解使用JRebel插件實(shí)現(xiàn)SpringBoot應(yīng)用代碼熱加載

    詳解使用JRebel插件實(shí)現(xiàn)SpringBoot應(yīng)用代碼熱加載

    這篇文章主要介紹了詳解使用JRebel插件實(shí)現(xiàn)SpringBoot應(yīng)用代碼熱加載,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 關(guān)于Java中try finally return語句的執(zhí)行順序淺析

    關(guān)于Java中try finally return語句的執(zhí)行順序淺析

    這篇文章主要介紹了關(guān)于Java中try finally return語句的執(zhí)行順序淺析,需要的朋友可以參考下
    2017-08-08

最新評(píng)論

玛多县| 恭城| 斗六市| 两当县| 资兴市| 盘锦市| 宁城县| 呈贡县| 黄冈市| 贵南县| 永定县| 江川县| 阿勒泰市| 郯城县| 九龙城区| 广州市| 祥云县| 交城县| 固镇县| 上蔡县| 华容县| 水城县| 海晏县| 涪陵区| 聂拉木县| 甘肃省| 库车县| 海兴县| 本溪市| 嘉善县| 龙游县| 来安县| 建瓯市| 云霄县| 哈巴河县| 青龙| 肥西县| 砚山县| 金川县| 夏邑县| 宜兴市|