[Spring] @SpringBootAppllication ์„ค๋ช…

@SpringBootApplication ์•ˆ์— ๋“ค์–ด์žˆ๋Š” @ComponentScan, @EnableAutoConfiguration ์„ ์•Œ์•„๋ณด์ž.

Spring Initializer๋ฅผ ํ†ตํ•ด์„œ ๋งŒ๋“  ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ Root Application ํด๋ž˜์Šค ๋ ˆ๋ฒจ์— @SpringBootApplication ์ด ๋ถ™์–ด์žˆ๋‹ค.

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }
}

@SpringBootApplication์€ ํฌ๊ฒŒ 3๊ฐ€์ง€์˜ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM,
                classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
  ... ์ƒ๋žต

@SpringBootConfiguration์€ @Configuration ์ด๋‹ค.

@ComponetScan์€ ์ปดํฌ๋„ŒํŠธ ๋นˆ๋“ค์„ ์Šค์บ๋‹ํ•ด์„œ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ปดํฌ๋„ŒํŠธ์— ํ•ด๋‹นํ•˜๋Š” ๋นˆ๋“ค์€ ํด๋ž˜์Šค ๋ ˆ๋ฒจ์— @RestController, @Service, @Repository, @Configuration, @Component ์™€ ๊ฐ™์ด ๋ถ™์—ฌ์ค€๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ ์ œ์ผ ์ค‘์š”ํ•œ @EnableAutoConfiguration ์€ spring-boot-autoconfigure๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ spring.factories ๋ผ๋Š” ๋ฉ”ํƒ€์ •๋ณด์— ๊ธฐ๋ก๋œ AutoConfiguration ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration
// ...

๊ฒฐ๊ตญ ์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ ๋นˆ์„ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ 2๊ฐ€์ง€๋กœ ๋‚˜๋‰œ๋‹ค.

  1. ๋ฒ ์ด์Šค ํŒจํ‚ค์ง€ ๊ธฐ๋ฐ˜์˜ ComponentScan์„ ํ†ตํ•ด์„œ ๋‚ด๊ฐ€ ์ •์˜ํ•œ ์ปดํฌ๋„ŒํŠธ(Repository, Controller, Service, Configuration, Component) ๋ฅผ ๋“ฑ๋กํ•œ๋‹ค.
  2. @EnableAutoConfiguration์„ ํ†ตํ•ด์„œ spring.factories์— ์ •์˜๋˜์–ด ์žˆ๋Š” Configuration๋นˆ๋“ค์„ ๋“ฑ๋กํ•œ๋‹ค.

์กฐ๊ธˆ๋” ์‚ดํŽด๋ณด์ž

@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
        TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

  @Bean
    @ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
    @ConditionalOnProperty(prefix = "spring.mvc.hiddenmethod.filter", name = "enabled",
            matchIfMissing = true)
    public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
        return new OrderedHiddenHttpMethodFilter();
    }

  ...์ƒ๋žต
}

์œ„์˜ AutoConfiguration์—์„œ WebMvcAutoConfiguration ํด๋ž˜์Šค๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

ํด๋ž˜์Šค ๋ ˆ๋ฒจ, ๋ฉ”์†Œ๋“œ ๋ ˆ๋ฒจ์— ๋ณด์ด๋Š”๊ฒŒ @ConditionalOn xxx ์ด๋Ÿฐ ์–ด๋…ธํ…Œ์ด์…˜์ด ๋งŽ์ด ๋ณด์ธ๋‹ค. on๋’ค์— ์–ด๋–ค ์กฐ๊ฑด์ธ ๊ฒฝ์šฐ์—์„œ Conditionalํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด @ConditionalOnMissingBean(HiddenHttpMethodFilter.class) HiddenHttpMethodFilter๊ฐ€ ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด, return new OrderedHiddenHttpMethodFilter(); ์ƒˆ๋กœ์šด ํ•„ํ„ฐ๋ฅผ ๋งŒ๋“ ๋‹ค.

์—ฐ๊ด€ ํฌ์ŠคํŠธ