国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

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

瀏覽:112日期: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
相關文章:
主站蜘蛛池模板: 晴隆县| 台中县| 红安县| 兰考县| 桐梓县| 巫溪县| 桐庐县| 昌乐县| 抚松县| 四子王旗| 溧水县| 桐乡市| 饶阳县| 天镇县| 黄龙县| 合江县| 龙江县| 神木县| 宜章县| 清水河县| 宁津县| 商洛市| 墨竹工卡县| 筠连县| 宁陕县| 柞水县| 白玉县| 萍乡市| 彭泽县| 锡林浩特市| 武定县| 元朗区| 青神县| 工布江达县| 通城县| 海盐县| 崇仁县| 乌兰浩特市| 砚山县| 茌平县| 清丰县|