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

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

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

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

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

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

值得關注的有SpringApplication.run以及注解@SpringBootApplication

run方法

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

獲取監聽器

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));// 創建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 { // 通過名字創建類的class對象 Class<?> instanceClass = ClassUtils.forName(name, classLoader); Assert.isAssignable(type, instanceClass); // 構造器獲取 Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes); // 創建具體實例 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());// 創建打印器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);}

最終輸出內容類: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底層有了一個大致了解,只分析了主要方法,希望對大家有幫助

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

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产高清视频a在线大全 | 日本免费人成在线网站 | 高清韩国a级特黄毛片 | 亚洲成在人线免费视频 | 欧美日韩精品高清一区二区 | 国产91丝袜美腿在线观看 | 中文字幕在线播放视频 | 国产精品变态重口在线 | 日本欧美视频 | 一区二区三区中文字幕 | 久草视频在线看 | 国产成人精品综合在线 | 亚洲www| 欧美性群另类交 | 精品在线视频观看 | 香港经典a毛片免费观看看 香港经典a毛片免费观看爽爽影院 | 国产成人艳妇在线观看 | 国产欧美日本在线观看 | 91av小视频 | 国产一区二区三区免费视频 | 成年人三级黄色片 | 亚洲日本va | 欧美一级特黄做 | 欧美日韩精品一区二区三区 | 午夜宅男在线永远免费观看网 | av大片| 91久久青青草原免费 | 91香焦国产线观看看免费 | 中文日韩字幕 | 在线播放精品一区二区啪视频 | 国产高清天干天天视频 | 成人爽a毛片在线视频网站 成人爽爽大片在线观看 | 国产一区在线免费观看 | 亚洲一区天堂 | 99久久免费午夜国产精品 | 久久99精品久久久久久 | aa级毛片| 一级aaaaa毛片免费视频 | 亚洲第一在线播放 | 99久久久久国产 | 成人a影片在线观看 |