๐ŸŒฟSpring/Spring Framework

[Spring Framework] Spring Boot์˜ ์‹œ์ž‘ ํ”„๋กœ์„ธ์Šค

Boom's 2024. 5. 13. 09:45
๋ฐ˜์‘ํ˜•

 

ํ”„๋ ˆ์ž„์›Œํฌ ์‹œ์ž‘ ํด๋ž˜์Šค

๋ชจ๋“  Spring Boot ํ”„๋กœ์ ํŠธ์—๋Š” @SpringBootApplication.

@SpringBootApplication 
๊ณต์šฉ  ํด๋ž˜์Šค  Application { 
    public  static  void  main (String[] args)  throws Exception { 
        SpringApplication.run(Application.class, args); 
    } 
}

๋ฉ”์†Œ๋“œ๋ฅผ ์ง์ ‘ ์‚ดํŽด๋ณด๋ฉด SpringApplication.run์ „์ฒด ์‹œ์ž‘ ํ”„๋กœ์„ธ์Šค๊ฐ€ SpringBootApplication ์ƒ์„ฑ์ž ์™€ run ๋ฉ”์†Œ๋“œ ์‹คํ–‰์ด๋ผ๋Š” ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

public  static ConfigurableApplicationContext run (Class<?>[] PrimarySources, String[] args) {   
 return  new  SpringApplication (primarySources).run(args); 
}

ํ•ต์‹ฌ ์ฃผ์„

์ฃผ์„์„ ์—ด๋ฉด @SpringBootApplication์—ฌ๋Ÿฌ ์ฃผ์„์ด ํ‘œ์‹œ๋˜์ง€๋งŒ ์„ธ ๊ฐ€์ง€ ํ•ต์‹ฌ ์ฃผ์„์— ์ง‘์ค‘ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

@SpringBootConfiguration

  1. ๋ณธ์งˆ์ ์œผ๋กœ ์ด๋Š” ์ฃผ์„์˜ ๋ž˜ํผ์ž…๋‹ˆ๋‹ค @Configuration. JavaConfig๋ฅผ ํ†ตํ•ด Bean์„ ๊ตฌ์„ฑํ•˜๊ณ  ํ˜„์žฌ ํด๋ž˜์Šค๋ฅผ ๊ตฌ์„ฑ ํด๋ž˜์Šค๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

@EnableAutoConfiguration

  1. ์ฃผ์„์„ ์—ด๋ฉด AutoConfigurationPackage ๋ฐ @Import(AutoConfigurationImportSelector.class)๋ผ๋Š”@EnableAutoConfiguration ๋‘ ๊ฐ€์ง€ ํ•ต์‹ฌ ์ฃผ์„์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.
  2. @AutoConfigurationPackage์ฃผ์„ ๊ณผ ์˜๋ฏธ๊ฐ€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค @Component. ํ•˜๋‚˜๋Š” ์ž๋™ ๊ตฌ์„ฑ ํด๋ž˜์Šค๋ฅผ ๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•ด ์ฃผ์„์„ ์ˆ˜๋™์œผ๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋ฉฐ, ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ์ง€์ •๋œ ๊ฒฝ๋กœ๋ฅผ ์Šค์บ”ํ•˜์—ฌ Bean์„ ์ฃผ์ž…ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. @AutoConfigurationPackage๋‹จ์ผ ๊ฒฝ๋กœ์— ๋กœ๋“œํ•  ์ž๋™ ๊ตฌ์„ฑ ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ์ฃผ์„์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ ํด๋ž˜์Šค๋ฅผ ๊ฐœ๋ณ„์ ์œผ๋กœ ์ถ”๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ @Import๊ฐ„๋‹จํžˆ ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด ๋” ํŽธ๋ฆฌํ•ด์ง‘๋‹ˆ๋‹ค. @ComponentScan์˜ˆ๋ฅผ ๋“ค์–ด ์ผ๋ถ€ Bean์ด SpringBootApplication๊ณผ ๋™์ผํ•œ ๊ฒฝ๋กœ์— ์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉ์ž ์ •์˜ํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
  3. @Import(AutoConfigurationImportSelector.class)AutoConfigurationImportSelectorํด๋ž˜์Šค, ํŠนํžˆ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค getCandidateConfigurations(). ์ด ๋ฉ”์†Œ๋“œ๋Š” SpringFactoriesLoader.loadFactoryNames()META-INF/spring.factories ํŒŒ์ผ์— ๋‚˜์—ด๋œ ๋ชจ๋“  ์ž๋™ ๊ตฌ์„ฑ ํด๋ž˜์Šค๋ฅผ ์ฐพ์•„ ๋กœ๋“œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

@ComponentScan

  1. ๊ธฐ๋ณธ์ ์œผ๋กœ ํ˜„์žฌ ํŒจํ‚ค์ง€์™€ ํ•ด๋‹น ํ•˜์œ„ ํŒจํ‚ค์ง€๋ฅผ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. @Component, @Controller, @Service, ๋“ฑ ์˜ ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค๋ฅผ @Repository์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋กํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์œ„์˜ ์ฃผ์„์€ ํ•„์š”ํ•œ ํด๋ž˜์Šค๋ฅผ ํด๋ž˜์Šค ๊ฒฝ๋กœ์— ๋กœ๋“œํ•  ๋ฟ์ž…๋‹ˆ๋‹ค. ํด๋ž˜์Šค๋ฅผ ํš๋“ํ•˜๊ณ  ์ด๋ฅผ Bean์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์‹ค์ œ ํ”„๋กœ์„ธ์Šค๋Š” run ๋ฉ”์†Œ๋“œ์—์„œ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด์— ๋Œ€ํ•œ ์งˆ๋ฌธ์— ๋‹ตํ•  ๋•Œ ๋จผ์ € ํ•ต์‹ฌ ์ฃผ์„์„ ์–ธ๊ธ‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ์‹œ์ž‘ ํ”„๋กœ์„ธ์Šค๋Š” ์—ฌ์ „ํžˆ SpringBootApplcation ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์™€ ์‹คํ–‰ ๋ฉ”์„œ๋“œ๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ

public  SpringApplication (ResourceLoaderresourceLoader, Class<?>...primarySources) { 
   this .resourceLoader =resourceLoader; 

   Assert.notNull(primarySources, "PrimarySources๋Š” null์ด ์•„๋‹ˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค." ); 

   this .primarySources = new  LinkedHashSet <>(Arrays.asList(primarySources)); 

   // ์›น ํ™˜๊ฒฝ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๊ฐ€ ๋กœ๋“œ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์œ ํ˜•์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. 
   this .webApplicationType = WebApplicationType.deduceFromClasspath(); 

   this .bootstrappers = new  ArrayList <>(getSpringFactoriesInstances(Bootstrapper.class)); 

   // META-INFO/spring.factories ํŒŒ์ผ์„ ์ฝ๊ณ  ํ•ด๋‹น ApplicationContextInitializer๋ฅผ ์ปฌ๋ ‰์…˜์œผ๋กœ ์–ด์…ˆ๋ธ”ํ•ฉ๋‹ˆ๋‹ค.
    setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class)); 

   // ๋ชจ๋“  ๋ฆฌ์Šค๋„ˆ ์„ค์ •
    setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); 

   // ๋ฉ”์ธ ๋ฉ”์†Œ๋“œ ์ถ”๋ก  
   this .mainApplicationClass = deduceMainApplicationClass(); 
}

์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ๋Š” ์ฃผ๋กœ ๋‹ค์Œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๊ฐ€ ๋กœ๋“œ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ์›น ํ™˜๊ฒฝ์ธ์ง€ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.
  2. ApplicationContextInitializer์•„๋ž˜ ์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๋ชจ๋“  ์ดˆ๊ธฐํ™” ํ”„๋กœ๊ทธ๋žจ์„ ์–ป์Šต๋‹ˆ๋‹ค META-INF/spring.factories. ๋ฆฌํ”Œ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค๋ฅผ ํš๋“ํ•˜๊ณ  Spring ์ธ์Šคํ„ด์Šค ์‹œ์ž‘ ์ „ํ›„์— ์ฝœ๋ฐฑ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  3. ๊ตฌ์„ฑ์„ ์Šค์บ”ํ•˜๊ณ  ํ•ด๋‹น ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ๋กœ๋“œํ•˜์—ฌ ์ง€์  2์™€ ์œ ์‚ฌํ•œ ๋ชจ๋“  ๋ฆฌ์Šค๋„ˆ๋ฅผ ํš๋“ํ•ฉ๋‹ˆ๋‹ค.
  4. ์ฃผ์š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

runโ€‹

/** 
* Spring ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜์—ฌ ์ƒˆ๋กœ์šด 
* { @link ApplicationContext}๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ƒˆ๋กœ ๊ณ ์นฉ๋‹ˆ๋‹ค. 
* 
* @param์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ธ์ˆ˜๋ฅผ ์ธ์ˆ˜ํ•ฉ๋‹ˆ๋‹ค(์ผ๋ฐ˜์ ์œผ๋กœ Java ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ์—์„œ ์ „๋‹ฌ๋จ) 
* @return a running { @link ApplicationContext} 
*/ 
public ConfigurableApplicationContext run (String... args) { 
    // ํ”„๋กœ์ ํŠธ ์‹œ์ž‘ ์‹œ๊ฐ„์„ ์ธก์ •ํ•˜๊ธฐ ์œ„ํ•ด ์Šคํ†ฑ์›Œ์น˜๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. 
    StopWatch  stopWatch  =  ์ƒˆ๋กœ์šด  StopWatch (); 
    stopWatch.start(); 
    // ๋ถ€ํŠธ์ŠคํŠธ๋žฉ ์ปจํ…์ŠคํŠธ ๊ฐ์ฒด, ์ฆ‰ Spring์˜ ๋ฃจํŠธ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 
    DefaultBootstrapContext  bootstrapContext  = createBootstrapContext(); 
    // ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ์— ๋Œ€ํ•œ ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. 
    ConfigurableApplicationContext  context  =  null ; 
    /** 
     * JDK ์‹œ์Šคํ…œ ์†์„ฑ ์„ค์ • 
     * 'ํ—ค๋“œ๋ฆฌ์Šค'๋Š” ๋ง ๊ทธ๋Œ€๋กœ ํ—ค๋“œ๋ฆฌ์Šค ๋ชจ๋“œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 
     * ์ด๋Š” Spring Boot๊ฐ€ ๋งˆ์šฐ์Šค ๋ฐ ํ‚ค๋ณด๋“œ ์ง€์›์ด ์—†๋Š” ํ™˜๊ฒฝ, 
     * ์ผ๋ฐ˜์ ์œผ๋กœ Linux์™€ ๊ฐ™์€ ์„œ๋ฒ„์—์„œ ์‹คํ–‰๋  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋จ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ธฐ๋ณธ๊ฐ’์€ true์ž…๋‹ˆ๋‹ค. 
     */
     configureHeadlessProperty(); 
    /** 
     * ์•ž์„œ ์–ธ๊ธ‰ํ•œ getSpringFactoriesInstances ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” getRunListeners๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹คํ–‰ ๋ฆฌ์Šค๋„ˆ๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. 
     * spring.factories์—์„œ ๊ตฌ์„ฑ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. 
     */ 
    SpringApplicationRunListeners  listenings  = getRunListeners(args); 
    // ๋ฆฌ์Šค๋„ˆ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค
     .listeners.starting(bootstrapContext, this .mainApplicationClass); 
    try { 
        // ๊ธฐ๋ณธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ธ์ˆ˜, ์ฆ‰ ๋ช…๋ น์ค„์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹œ์ž‘ํ•  ๋•Œ ์ „๋‹ฌ๋˜๋Š” ์ธ์ˆ˜๋ฅผ ๋ž˜ํ•‘ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ: --server.port=9000 
        ApplicationArguments  applicationArguments  =  new  DefaultApplicationArguments (args); 
        // 
        /** * prepareEnvironment๋Š”          * getOrCreateEnvironment,configureEnvironment,configurePropertySources,configureProfiles,          *environmentPrepared,bindToSpringApplication,attach ๋ฐ ์•„๋ž˜ ์˜ˆ์ œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ธฐํƒ€ ๋ฉ”์†Œ๋“œ๋ฅผ 
         ํฌํ•จํ•˜๋Š” ์ค‘์š”ํ•œ ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค.          */ 



        ConfigurableEnvironment  ํ™˜๊ฒฝ  = prepareEnvironment(listeners, bootstrapContext , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ธ์ˆ˜); 
        // ๋ฌด์‹œ๋œ ๋นˆ์„ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.
        configureIgnoreBeanInfo(ํ™˜๊ฒฝ); 
        // ์‹œ์ž‘ ์‹œ ์ฝ˜์†”์— ํ‘œ์‹œ๋˜๋Š” ๋กœ๊ณ ์ธ Spring Boot ๋ฐฐ๋„ˆ๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค. src/main/resources์— ๋ฐฐ๋„ˆ๋ผ๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ํŒŒ์ผ์„ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 
        Banner  PrintedBanner  = printBanner(environment); 
        // IOC ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ
         ์ปจํ…์ŠคํŠธ = createApplicationContext(); 
        // ์‹คํ–‰ ํ”„๋กœ๊ทธ๋žจ์„ ์„ค์ •ํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์ž‘์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
         context.setApplicationStartup( this .applicationStartup); 
        // IOC ์ปจํ…Œ์ด๋„ˆ ๊ธฐ๋ณธ์ •๋ณด ์„ค์ •(Spring ์ปจํ…Œ์ด๋„ˆ ์ „์ฒ˜๋ฆฌ)
         prepareContext(bootstrapContext, context, Environment, listeningrs, applicationArguments, PrintedBanner); 
        /** 
         * IOC ์ปจํ…Œ์ด๋„ˆ ์ƒˆ๋กœ ๊ณ ์นจ 
         * ์—ฌ๊ธฐ์—๋Š” Spring ์ปจํ…Œ์ด๋„ˆ ์‹œ์ž‘, ์ž๋™ ๋ฐฐ์„  ํ™œ์„ฑํ™”, ์›น ์„œ๋น„์Šค๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ WebServer ์ƒ์„ฑ, ์ฆ‰ Spring Boot์— ๋‚ด์žฅ๋œ Tomcat ์‹คํ–‰์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. 
         */
         refreshContext(context); 
        /** 
         * ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒˆ๋กœ ๊ณ ์ณ์ง„ ํ›„ ์‚ฌ์šฉ์ž๊ฐ€ ์‚ฌ์šฉ์ž ์ •์˜ ๋กœ์ง์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ—ˆ์šฉ 
         * ์ƒˆ๋กœ ๊ณ ์นจ ํ›„ Spring ์ปจํ…Œ์ด๋„ˆ์˜ ํ›„์ฒ˜๋ฆฌ 
         */
         afterRefresh(context, applicationArguments); 
        // ํƒ€์ด๋จธ๋ฅผ ์ค‘์ง€ํ•˜๊ณ  ์‹œ์ž‘ ํ›„ ์ฝ˜์†”์— ํ‘œ์‹œ๋˜๋Š” ์‹œ๊ฐ„์„ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.
         stopWatch.stop(); 
        if ( this .logStartupInfo) { 
            // ์‹œ์ž‘์ด ์™„๋ฃŒ๋˜๋ฉด ๋‚˜ํƒ€๋‚˜๋Š” ๋กœ๊ทธ๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค 
            new  StartupInfoLogger ( this .mainApplicationClass).logStarted(getApplicationLog(), stopWatch); 
        } 
        // ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๊ฐ€ ์‹œ์ž‘๋˜์—ˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ด๋ฒคํŠธ ๊ฒŒ์‹œ; ๋ชจ๋“  ์‹คํ–‰ ๋ฆฌ์Šค๋„ˆ๋Š” ์‹œ์ž‘๋œ() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค
         .listeners.started(context); 
        // ๋ชจ๋“  ์‹คํ–‰๊ธฐ๋ฅผ ๋ฐ˜๋ณตํ•˜๊ณ  ํ•ด๋‹น ์‹คํ–‰ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‹คํ–‰๊ธฐ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
         callRunners(context, applicationArguments); 
    } catch (Throwable ex) { 
        // ์‹คํ–‰ ํ”„๋กœ์„ธ์Šค ์ค‘ ์˜ค๋ฅ˜์— ๋Œ€ํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
         handlerRunFailure(context, ex, listenings); ์ƒˆ๋กœ์šด IllegalStateException์„ 
        ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค  (์˜ˆ);     } try { // ๋ชจ๋“  ์‹คํ–‰ ๋ฆฌ์Šค๋„ˆ๋Š” running() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์ˆ˜์‹ ํ•ฉ๋‹ˆ๋‹ค         .listeners.running(context);     } catch (Throwable ex) { // ์˜ˆ์™ธ ์ฒ˜๋ฆฌ         handlerRunFailure(context, ex, null ); ์ƒˆ๋กœ์šด IllegalStateException์„ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค (์˜ˆ);     } 


    
        


        

          

    // ์ตœ์ข… ๋นŒ๋“œ๋œ ์ปจํ…Œ์ด๋„ˆ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค . 
    return context; 


}


๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ AbstractApplicationContext.refresh๋ฐฉ๋ฒ•๊ณผ ๊ด€๋ จ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ด run๋ฐฉ๋ฒ•์€ ์ฃผ๋กœ ๊ตฌ์„ฑ ํ™˜๊ฒฝ, ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ”„๋กœ์„ธ์Šค ๋‚ด์—์„œ ์ด refresh๋ฉ”์†Œ๋“œ๋Š” Spring Bean์˜ ๋ผ์ดํ”„์‚ฌ์ดํด์— ์นจํˆฌํ•˜์—ฌ Bean์— ๋Œ€ํ•œ ๋ผ์ดํ”„์‚ฌ์ดํด ์ „ํ›„ ํ›„ํฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  Spring ์ฃผ์„์œผ๋กœ ์ฃผ์„์ด ๋‹ฌ๋ฆฐ ํด๋ž˜์Šค๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด onRefresh๋‹จ๊ณ„์—์„œ๋Š” Java ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Tomcat ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ตฌ์„ฑ๋˜๊ณ  ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ•refreshโ€‹

๋„ˆ๋ฌด ๋งŽ์€ ์ฝ”๋“œ๋ฅผ ๊ธฐ์–ตํ•˜๊ธฐ ํž˜๋“ค๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์— ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ๋„ฃ๋Š” ๋Œ€์‹  ๋ช‡ ๋ฌธ์žฅ์œผ๋กœ ์š”์•ฝํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ „๋ฐ˜์ ์œผ๋กœ ์ด refresh๋ฐฉ๋ฒ•์€ ๋นˆ์˜ ์ˆ˜๋ช…์ฃผ๊ธฐ์— ๊ฑธ์ณ ์žˆ์Šต๋‹ˆ๋‹ค.

invokeBeanFactoryPostProcessors

  1. ์ด ๋ฉ”์†Œ๋“œ๋Š” BeanDefinitionRegistryPostProcessor ๋ฐ BeanFactoryPostProcessorinvokeBeanFactoryPostProcessors ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” Bean ํŒฉํ† ๋ฆฌ์˜ ๋ชจ๋“  Bean์„ ์‹๋ณ„ํ•ฉ๋‹ˆ๋‹ค . ๊ทธ๋Ÿฐ ๋‹ค์Œ ํฌ์ŠคํŠธ ํ”„๋กœ์„ธ์„œ ๋‚ด์—์„œ ๋ฐ ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.postProcessBeanDefinitionRegistry()postProcessBeanFactory()
  2. ์ด ๋ฉ”์„œ๋“œ๋Š” , ๋“ฑ ์˜ doProcessConfigurationClassSpring Boot ์ฃผ์„์ด ๋‹ฌ๋ฆฐ ๋ชจ๋“  ํด๋ž˜์Šค๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.@Import@Bean
  3. BeanDefinitionRegistryPostProcessor ๊ตฌํ˜„์€ ์ปจํ…Œ์ด๋„ˆ์— ๋นˆ ์ •์˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ˜๋ฉด BeanFactoryPostProcessor ๊ตฌํ˜„์€ ์ปจํ…Œ์ด๋„ˆ ๋‚ด์˜ ๋นˆ ์ •์˜์— ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. (์ด๋Š” Spring ๋นˆ์˜ ๋ผ์ดํ”„์‚ฌ์ดํด๊ณผ ๊ด€๋ จ์ด ์žˆ์Šต๋‹ˆ๋‹ค.)

onRefresh

์ด ๋‹จ๊ณ„์—๋Š” ์›น ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์›น ํ™˜๊ฒฝ์—์„œ ์ด๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ Tomcat ์›น ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.”

์š”์•ฝ

Spring Boot ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์ž‘์˜ ์ „์ฒด ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค.

  1. ์ „์ฒด Spring ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์‹œ์ž‘์€ ๋ฉ”์†Œ๋“œ constructing a SpringApplication object์‹คํ–‰์˜ ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.run
  2. ํ•ต์‹ฌ ์ฃผ์„์€ @SpringBootConfiguration์‹œ์ž‘ ํด๋ž˜์Šค๋ฅผ ๊ตฌ์„ฑ ํด๋ž˜์Šค๋กœ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. @EnableAutoConfiguration๋‚ด๋ถ€ @Import์ฃผ์„์„ ํ†ตํ•ด ์ž๋™ ๊ตฌ์„ฑ์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค AutoConfigurationImportSelector.class. @ComponentScan๊ธฐ๋ณธ์ ์œผ๋กœ ํ˜„์žฌ ๋””๋ ‰ํ„ฐ๋ฆฌ์™€ ํ•ด๋‹น ํ•˜์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ Bean์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
  3. SpringApplication์˜ ์ƒ์„ฑ์ž๋Š” ์ฃผ๋กœ ๋ช‡ ๊ฐ€์ง€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค ๋กœ๋”ฉ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์›น ํ™˜๊ฒฝ์ธ์ง€ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.
  • Spring ์ธ์Šคํ„ด์Šค ์‹œ์ž‘ ์ „ํ›„์— ์ผ๋ถ€ ์ฝœ๋ฐฑ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋“  ApplicationContextInitializerํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ  ๋ฆฌํ”Œ๋ ‰์…˜์„ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ชจ๋“  ์ดˆ๊ธฐํ™” ํ”„๋กœ๊ทธ๋žจ์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.META-INF/spring.factories
  • ์œ„์™€ ์œ ์‚ฌํ•œ ๋ชจ๋“  ๋ฆฌ์Šค๋„ˆ๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๊ตฌ์„ฑ์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ํ•ด๋‹น ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
  • ์ฃผ์š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
๋ฐ˜์‘ํ˜•