色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

SpringBoot上下文初始器加載過程詳解

瀏覽:88日期:2023-05-14 09:50:57

利用 Spring 工廠加載機制,實例化 ApplicationContextInitializer 實現類,并排序對象集合。

關鍵方法

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

技術實現步驟

實現類: org.springframework.core.io.support.SpringFactoriesLoader 配置資源: META-INF/spring.factories 排序: AnnotationAwareOrderComparator#sort

自定義初始化器

編寫類實現ApplicationContextInitializer接口

有兩種指定順序的方法,第一種:類上加注解@Order(Ordered.HIGHEST_PRECEDENCE),第二種則是實現Ordered接口

@Order(Ordered.HIGHEST_PRECEDENCE)public class HelloWorldApplicationContextInitializer<C extends ConfigurableApplicationContext> implements ApplicationContextInitializer<C> { @Override public void initialize(C applicationContext) { System.out.println('ConfigurableApplicationContext.id = '+ applicationContext.getId()); }}

public class AfterHelloWorldApplicationContextInitializer implements ApplicationContextInitializer, Ordered { @Override public void initialize(ConfigurableApplicationContext applicationContext) { System.out.println('After application.id = ' + applicationContext.getId()); } @Override public int getOrder() { return Ordered.LOWEST_PRECEDENCE; }}

在spring.properties中配置

# ApplicationContextInitializerorg.springframework.context.ApplicationContextInitializer=com.imooc.diveinspringboot.context.AfterHelloWorldApplicationContextInitializer,com.imooc.diveinspringboot.context.HelloWorldApplicationContextInitializer

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 免费一级大片 | 久久久精品久久久久久久久久久 | 成人永久免费视频 | 真人一级毛片国产 | 国产在亚洲线视频观看 | 国产理论在线观看 | 美女被免费视频网站a国产 美女被免费网站视频软件 美女被免费网站在线软件 美女被免费网站在线视频软件 | 中文字幕在线一区二区在线 | 一本色道久久综合网 | 欧美操操操操 | 高h原耽肉汁动漫视频 | 亚洲欧美日韩综合在线一区二区三区 | 老头巨大粗长xxxxx | 精品91精品91精品国产片 | 久久久久久久久久久福利观看 | 亚洲国产二区三区久久 | 男操女视频 | 亚洲综合精品一区二区三区中文 | 玖草在线| 国产一级强片在线观看 | 深爱五月开心网亚洲综合 | 日韩精品一区二区三区 在线观看 | 天堂8中文在线最新版在线 天堂8资源8在线 | 亚洲网址在线 | 欧美一级毛片无遮挡 | 91在线成人 | 99免费在线观看视频 | 免费观看一级成人毛片 | 免费观看成人毛片 | 中文字幕在线看 | 日韩精品一区二区三区 在线观看 | 青青操在线视频 | 国产三级做爰在线观看 | 亚洲影视一区二区 | 久久99视频精品 | 91精品视频在线播放 | 中文字幕国产专区 | 窝窝午夜精品一区二区 | 久久精品99精品免费观看 | 精品视频99| 黄页美女|