Run Spring

์Šคํ”„๋ง์„ ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

public ConfigurableApplicationContext run(String... args) {
	Startup startup = Startup.create();
	if (this.registerShutdownHook) {
		SpringApplication.shutdownHook.enableShutdownHookAddition();
	}
	DefaultBootstrapContext bootstrapContext = createBootstrapContext();
	ConfigurableApplicationContext context = null;
	configureHeadlessProperty();
	SpringApplicationRunListeners listeners = getRunListeners(args);
	listeners.starting(bootstrapContext, this.mainApplicationClass);
	try {
		ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
		ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
		Banner printedBanner = printBanner(environment);
		context = createApplicationContext();
		context.setApplicationStartup(this.applicationStartup);
		prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
		refreshContext(context);
		afterRefresh(context, applicationArguments);
		startup.started();
		if (this.logStartupInfo) {
			new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), startup);
		}
		listeners.started(context, startup.timeTakenToStarted());
		callRunners(context, applicationArguments);
	}
	catch (Throwable ex) {
		throw handleRunFailure(context, ex, listeners);
	}
	try {
		if (context.isRunning()) {
			listeners.ready(context, startup.ready());
		}
	}
	catch (Throwable ex) {
		throw handleRunFailure(context, ex, null);
	}
	return context;
}

์•Œ๊ฒ ์Šต๋‹ˆ๋‹ค! ๋ฒˆํ˜ธ ์—†์ด ์ •๋ฆฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


Startup startup = Startup.create(); ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹คํ–‰ ์‹œ์ž‘ ์‹œ์ ์„ ๊ธฐ๋กํ•˜๊ธฐ ์œ„ํ•œ Startup ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์‹คํ–‰ ์‹œ๊ฐ„์„ ์ถ”์ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

if (this.registerShutdownHook) ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ ์‹œ ์•ˆ์ „ํ•˜๊ฒŒ ์ข…๋ฃŒ๋  ์ˆ˜ ์žˆ๋„๋ก JVM์˜ shutdown hook์„ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค. ์ด hook์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ข…๋ฃŒ๋  ๋•Œ ํ˜ธ์ถœ๋˜์–ด ๋ฆฌ์†Œ์Šค ์ •๋ฆฌ ๋ฐ ๋นˆ ์ข…๋ฃŒ ๋“ฑ์˜ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

DefaultBootstrapContext bootstrapContext = createBootstrapContext(); ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ ๊ณผ์ •์—์„œ ํ•„์š”ํ•œ ๊ฐ์ฒด๋“ค์„ ์ €์žฅํ•˜๋Š” DefaultBootstrapContext ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ปจํ…์ŠคํŠธ๊ฐ€ ์™„์ „ํžˆ ์ดˆ๊ธฐํ™”๋˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

configureHeadlessProperty(); ํ—ค๋“œ๋ฆฌ์Šค ๋ชจ๋“œ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์„œ๋ฒ„ ํ™˜๊ฒฝ์—์„œ UI๊ฐ€ ์—†๋Š” ์ƒํƒœ๋กœ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋˜๋„๋ก ์„ค์ •ํ•˜๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค.

SpringApplicationRunListeners listeners = getRunListeners(args); ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ ๊ณผ์ •์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฆฌ์Šค๋„ˆ๋“ค์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ๋ช… ์ฃผ๊ธฐ ๋™์•ˆ ๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

listeners.starting(bootstrapContext, this.mainApplicationClass); ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋˜์—ˆ์Œ์„ ๋ฆฌ์Šค๋„ˆ๋“ค์—๊ฒŒ ์•Œ๋ฆฝ๋‹ˆ๋‹ค. BootstrapContext์™€ ๋ฉ”์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํด๋ž˜์Šค ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); ์ปค๋งจ๋“œ๋ผ์ธ์—์„œ ์ „๋‹ฌ๋œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ธ์ž๋“ค์„ ApplicationArguments ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ ์ค‘์— ํ•„์š”ํ•  ์ˆ˜ ์žˆ๋Š” ์ธ์ž ์ •๋ณด๋“ค์„ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments); ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ™˜๊ฒฝ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœํŒŒ์ผ, ํ”„๋กœํผํ‹ฐ ์†Œ์Šค ๋“ฑ์„ ์„ค์ •ํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ ํ™˜๊ฒฝ์„ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.

Banner printedBanner = printBanner(environment); ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์ž‘ ์‹œ ์ฝ˜์†”์— ์ถœ๋ ฅ๋˜๋Š” Spring ๋ฐฐ๋„ˆ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

context = createApplicationContext(); ApplicationContext๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. Spring์˜ ํ•ต์‹ฌ ์ปจํ…Œ์ด๋„ˆ๋กœ, ๋ชจ๋“  ๋นˆ๊ณผ ์„ค์ • ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

context.setApplicationStartup(this.applicationStartup); ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹œ์ž‘ ์ •๋ณด๋ฅผ ApplicationContext์— ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner); ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , Environment, ๋ฆฌ์Šค๋„ˆ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ธ์ž ๋“ฑ์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.

refreshContext(context); ApplicationContext๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•˜์—ฌ ๋ชจ๋“  ๋นˆ์ด ์ดˆ๊ธฐํ™”๋˜๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ค€๋น„๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

afterRefresh(context, applicationArguments); ์ปจํ…์ŠคํŠธ ์ƒˆ๋กœ๊ณ ์นจ ์ดํ›„ ์ถ”๊ฐ€์ ์ธ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€์ ์ธ ์ดˆ๊ธฐํ™”๋‚˜ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ๋“ฑ์ด ํฌํ•จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

startup.started(); ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์„ฑ๊ณต์ ์œผ๋กœ ์‹œ์ž‘๋˜์—ˆ์Œ์„ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

if (this.logStartupInfo) ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹œ์ž‘ ์ •๋ณด๋ฅผ ๋กœ๊ทธ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋œ ๋ฉ”์ธ ํด๋ž˜์Šค, ์‹คํ–‰ ์‹œ๊ฐ„ ๋“ฑ์„ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

listeners.started(context, startup.timeTakenToStarted()); ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋˜์—ˆ๋‹ค๋Š” ์ด๋ฒคํŠธ๋ฅผ ๋ฆฌ์Šค๋„ˆ๋“ค์—๊ฒŒ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

callRunners(context, applicationArguments); ApplicationRunner ๋˜๋Š” CommandLineRunner ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๋นˆ๋“ค์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋นˆ๋“ค์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋œ ํ›„ ์ถ”๊ฐ€์ ์ธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

catch (Throwable ex) ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ ์ค‘ ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ , ์‹คํŒจ ์‹œ ์ ์ ˆํ•˜๊ฒŒ ๋ฆฌ์†Œ์Šค๋ฅผ ์ •๋ฆฌํ•˜๊ฑฐ๋‚˜ ์ข…๋ฃŒ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

Last updated