电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

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

瀏覽:105日期: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)聽器正式運(yù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)文章:
主站蜘蛛池模板: 复合土工膜厂家|hdpe防渗土工膜|复合防渗土工布|玻璃纤维|双向塑料土工格栅-安徽路建新材料有限公司 | 石栏杆_青石栏杆_汉白玉栏杆_花岗岩栏杆 - 【石雕之乡】点石石雕石材厂 | 牛皮纸|牛卡纸|进口牛皮纸|食品级牛皮纸|牛皮纸厂家-伽立实业 | 【法利莱住人集装箱厂家】—活动集装箱房,集装箱租赁_大品牌,更放心 | 深圳市索富通实业有限公司-可燃气体报警器 | 可燃气体探测器 | 气体检测仪 | 精密线材测试仪-电线电缆检测仪-苏州欣硕电子科技有限公司 | 无线遥控更衣吊篮_IC卡更衣吊篮_电动更衣吊篮配件_煤矿更衣吊篮-力得电子 | 不锈钢复合板厂家_钛钢复合板批发_铜铝复合板供应-威海泓方金属复合材料股份有限公司 | 钢格板|镀锌钢格板|热镀锌钢格板|格栅板|钢格板|钢格栅板|热浸锌钢格板|平台钢格板|镀锌钢格栅板|热镀锌钢格栅板|平台钢格栅板|不锈钢钢格栅板 - 专业钢格板厂家 | 生物制药洁净车间-GMP车间净化工程-食品净化厂房-杭州波涛净化设备工程有限公司 | 龙门加工中心-数控龙门加工中心厂家价格-山东海特数控机床有限公司_龙门加工中心-数控龙门加工中心厂家价格-山东海特数控机床有限公司 | 玉米深加工机械,玉米加工设备,玉米加工机械等玉米深加工设备制造商-河南成立粮油机械有限公司 | 桨叶搅拌机_螺旋挤压/方盒旋切造粒机厂家-无锡市鸿诚输送机械有限公司 | 乳化沥青设备_改性沥青设备_沥青加温罐_德州市昊通路桥工程有限公司 | 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | LED太阳能中国结|发光红灯笼|灯杆造型灯|节日灯|太阳能灯笼|LED路灯杆装饰造型灯-北京中海轩光电 | 压砖机、液压制砖机、静压砖机、环保砖机生产厂家—杜甫机械 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 山西3A认证|太原AAA信用认证|投标AAA信用证书-山西AAA企业信用评级网 | 数显恒温培养摇床-卧式/台式恒温培养摇床|朗越仪器 | 水压力传感器_数字压力传感器|佛山一众传感仪器有限公司|首页 | 西门子伺服电机维修,西门子电源模块维修,西门子驱动模块维修-上海渠利 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | 清洁设备_洗地机/扫地机厂家_全自动洗地机_橙犀清洁设备官网 | lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 锡膏喷印机-全自动涂覆机厂家-全自动点胶机-视觉点胶机-深圳市博明智控科技有限公司 | 七维官网-水性工业漆_轨道交通涂料_钢结构漆 | 电机铸铝配件_汽车压铸铝合金件_发动机压铸件_青岛颖圣赫机械有限公司 | 碳纤维复合材料制品生产定制工厂订制厂家-凯夫拉凯芙拉碳纤维手机壳套-碳纤维雪茄盒外壳套-深圳市润大世纪新材料科技有限公司 | 创绿家招商加盟网-除甲醛加盟-甲醛治理加盟-室内除甲醛加盟-创绿家招商官网 | 回转窑-水泥|石灰|冶金-巩义市瑞光金属制品有限责任公司 | 【连江县榕彩涂料有限公司】官方网站| 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | 抖音短视频运营_企业网站建设_网络推广_全网自媒体营销-东莞市凌天信息科技有限公司 | 捷码低代码平台 - 3D数字孪生_大数据可视化开发平台「免费体验」 | 沈阳真空机_沈阳真空包装机_沈阳大米真空包装机-沈阳海鹞真空包装机械有限公司 | 自动检重秤-动态称重机-重量分选秤-苏州金钻称重设备系统开发有限公司 | 沈阳液压泵_沈阳液压阀_沈阳液压站-沈阳海德太科液压设备有限公司 | 药品/药物稳定性试验考察箱-埃里森仪器设备(上海)有限公司 | 好物生环保网、环保论坛 - 环保人的学习交流平台 | 铸铝门厂家,别墅大门庭院大门,别墅铸铝门铜门[十大品牌厂家]军强门业 |