av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot 啟動方法run()源碼解析

瀏覽:111日期:2023-03-19 17:41:55
入口

通常一個簡單的SpringBoot基礎(chǔ)項目我們會有如下代碼

@SpringBootApplication@RestController@RequestMapping('/')public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

值得關(guān)注的有SpringApplication.run以及注解@SpringBootApplication

run方法

public ConfigurableApplicationContext run(String... args) { // 秒表StopWatch stopWatch = new StopWatch();stopWatch.start();ConfigurableApplicationContext context = null;Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();configureHeadlessProperty();// 獲取監(jiān)聽器SpringApplicationRunListeners listeners = getRunListeners(args);// 監(jiān)聽器啟動listeners.starting();try { // application 啟動參數(shù)列表ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);// 配置忽略的bean信息configureIgnoreBeanInfo(environment);Banner printedBanner = printBanner(environment);// 創(chuàng)建應(yīng)用上下文context = createApplicationContext();exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,new Class[] { ConfigurableApplicationContext.class }, context); // 準(zhǔn)備上下文,裝配beanprepareContext(context, environment, listeners, applicationArguments, printedBanner);// 上下文刷新refreshContext(context);// 刷新后做什么afterRefresh(context, applicationArguments);stopWatch.stop();if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);}// 監(jiān)聽器開始了listeners.started(context);// 喚醒callRunners(context, applicationArguments);}catch (Throwable ex) {handleRunFailure(context, ex, exceptionReporters, listeners);throw new IllegalStateException(ex);}try { // 監(jiān)聽器正式運行l(wèi)isteners.running(context);}catch (Throwable ex) {handleRunFailure(context, ex, exceptionReporters, null);throw new IllegalStateException(ex);}return context;}getRunListeners

獲取監(jiān)聽器

private SpringApplicationRunListeners getRunListeners(String[] args) {Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };// 獲取 Spring Factory 實例對象return new SpringApplicationRunListeners(logger,getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args));}private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {ClassLoader classLoader = getClassLoader();// Use names and ensure unique to protect against duplicates// 讀取 spring.factoriesSet<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));// 創(chuàng)建SpringFactory實例List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);/** * 排序 {@link Ordered} */AnnotationAwareOrderComparator.sort(instances);return instances;}

createSpringFactoriesInstances

@SuppressWarnings('unchecked') private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, ClassLoader classLoader, Object[] args, Set<String> names) { // 初始化 List<T> instances = new ArrayList<>(names.size()); for (String name : names) { try { // 通過名字創(chuàng)建類的class對象 Class<?> instanceClass = ClassUtils.forName(name, classLoader); Assert.isAssignable(type, instanceClass); // 構(gòu)造器獲取 Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes); // 創(chuàng)建具體實例 T instance = (T) BeanUtils.instantiateClass(constructor, args); // 加入實例表中 instances.add(instance); } catch (Throwable ex) { throw new IllegalArgumentException('Cannot instantiate ' + type + ' : ' + name, ex); } } return instances; }printBanner

private Banner printBanner(ConfigurableEnvironment environment) {if (this.bannerMode == Banner.Mode.OFF) {return null;}ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader: new DefaultResourceLoader(getClassLoader());// 創(chuàng)建打印器SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, this.banner);if (this.bannerMode == Mode.LOG) { // 輸出return bannerPrinter.print(environment, this.mainApplicationClass, logger);} // 輸出return bannerPrinter.print(environment, this.mainApplicationClass, System.out);}Banner print(Environment environment, Class<?> sourceClass, PrintStream out) {Banner banner = getBanner(environment);banner.printBanner(environment, sourceClass, out);return new PrintedBanner(banner, sourceClass);}

最終輸出內(nèi)容類:org.springframework.boot.SpringBootBanner

class SpringBootBanner implements Banner {private static final String[] BANNER = { '', ' . ____ _ __ _ _',' / / ___’_ __ _ _(_)_ __ __ _ ', '( ( )___ | ’_ | ’_| | ’_ / _` | ',' / ___)| |_)| | | | | || (_| | ) ) ) )', ' ’ |____| .__|_| |_|_| |___, | / / / /',' =========|_|==============|___/=/_/_/_/' };private static final String SPRING_BOOT = ' :: Spring Boot :: ';private static final int STRAP_LINE_SIZE = 42;@Overridepublic void printBanner(Environment environment, Class<?> sourceClass, PrintStream printStream) {for (String line : BANNER) {printStream.println(line);}String version = SpringBootVersion.getVersion();version = (version != null) ? ' (v' + version + ')' : '';StringBuilder padding = new StringBuilder();while (padding.length() < STRAP_LINE_SIZE - (version.length() + SPRING_BOOT.length())) {padding.append(' ');}printStream.println(AnsiOutput.toString(AnsiColor.GREEN, SPRING_BOOT, AnsiColor.DEFAULT, padding.toString(),AnsiStyle.FAINT, version));printStream.println();}}

希望通過本篇對于springboot啟動方法的解讀,讓大家對springboot底層有了一個大致了解,只分析了主要方法,希望對大家有幫助

到此這篇關(guān)于SpringBoot 啟動方法run()源碼賞析的文章就介紹到這了,更多相關(guān)SpringBoot 啟動run()內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 福利视频网站 | 一区二区三区免费 | 请别相信他免费喜剧电影在线观看 | 99精品国自产在线观看 | 2019天天干夜夜操 | 亚洲成人免费在线 | 殴美黄色录像 | 久久91精品久久久久久9鸭 | 国产精品1区2区 | 国产高清在线视频 | 欧美 日韩 国产 在线 | 久久久久久高潮国产精品视 | 在线视频一区二区三区 | 欧美日韩久久久久 | 欧美精品成人影院 | 日韩精品 电影一区 亚洲 | 成人h电影在线观看 | 亚洲色图第一页 | 久久久久久免费毛片精品 | 激情91 | 成人av电影在线 | 亚洲女人天堂网 | 久久成人久久 | 极品国产视频 | 国产免费一区二区三区免费视频 | 国产高清免费 | 中文字幕在线一区二区三区 | 欧美9999 | 一区二区三区视频在线 | 999视频在线播放 | 中文字幕综合 | 狠狠色香婷婷久久亚洲精品 | 国产精品视频一二三区 | 成人免费大片黄在线播放 | 国产欧美日韩一区二区三区在线 | 日韩欧美理论片 | 国产一区二区视频免费在线观看 | 免费观看的黄色网址 | 欧美精品福利 | 亚洲人成在线观看 | 三级黄片毛片 |