欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年03月25日 08:39:43   作者:碼上代碼  
這篇文章主要介紹了SpringBoot 啟動方法run()源碼賞析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

入口

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

@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)備上下文,裝配bean
			prepareContext(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)行
			listeners.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 實(shí)例對象
		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.factories
		Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
		// 創(chuàng)建SpringFactory實(shí)例
		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)建具體實(shí)例
 			T instance = (T) BeanUtils.instantiateClass(constructor, args);
 			// 加入實(shí)例表中
 			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;

	@Override
	public 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底層有了一個(gè)大致了解,只分析了主要方法,希望對大家有幫助

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

相關(guān)文章

  • java后端+前端使用WebSocket實(shí)現(xiàn)消息推送的詳細(xì)流程

    java后端+前端使用WebSocket實(shí)現(xiàn)消息推送的詳細(xì)流程

    后端向前端推送消息就需要長連接,首先想到的就是websocket,下面這篇文章主要給大家介紹了關(guān)于java后端+前端使用WebSocket實(shí)現(xiàn)消息推送的詳細(xì)流程,需要的朋友可以參考下
    2022-10-10
  • Java詳細(xì)講解Math和Random類中有哪些常用方法

    Java詳細(xì)講解Math和Random類中有哪些常用方法

    Math類位于java.lang包中,包含很多用于科學(xué)計(jì)算的類方法,這些方法可以直接通過類名調(diào)用。Random類獲取隨機(jī)數(shù),位于java.util包中,本篇帶你了解它們的常用方法
    2022-05-05
  • 深入理解java中i++和++i的區(qū)別

    深入理解java中i++和++i的區(qū)別

    下面小編就為大家?guī)硪黄钊肜斫鈐ava中i++和++i的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12
  • Idea配置超詳細(xì)圖文教程(2020.2版本)

    Idea配置超詳細(xì)圖文教程(2020.2版本)

    這篇文章主要介紹了Idea配置超詳細(xì)圖文教程(2020.2版本),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • JAVA線程池監(jiān)控以及動態(tài)調(diào)整示例詳解

    JAVA線程池監(jiān)控以及動態(tài)調(diào)整示例詳解

    這篇文章主要為大家介紹了JAVA線程池監(jiān)控以及動態(tài)調(diào)整示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • java的各種類型轉(zhuǎn)換全部匯總(推薦)

    java的各種類型轉(zhuǎn)換全部匯總(推薦)

    下面小編就為大家?guī)硪黄猨ava的各種類型轉(zhuǎn)換全部匯總(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-05-05
  • springboot 通過代碼自動生成pid的方法

    springboot 通過代碼自動生成pid的方法

    這篇文章主要介紹了springboot 通過代碼自動生成pid的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • spring緩存自定義resolver的方法

    spring緩存自定義resolver的方法

    這篇文章主要為大家詳細(xì)介紹了spring緩存自定義resolver的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Java中的定時(shí)器Timer詳解

    Java中的定時(shí)器Timer詳解

    這篇文章主要為大家詳細(xì)介紹了Java定時(shí)器Timer的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Spring Boot如何使用HikariCP連接池詳解

    Spring Boot如何使用HikariCP連接池詳解

    這篇文章主要給大家介紹了關(guān)于Spring Boot如何使用HikariCP連接池的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用springboot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評論