๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ ๋ฐฑ์—”๋“œ/SpringBoot

[์Šคํ”„๋ง๋ถ€ํŠธ ํ€ต์Šคํƒ€ํŠธ] ch.2 ์˜์กด์„ฑ๊ณผ ์ž๋™์„ค์ •

by OR15A 2023. 11. 23.
๊ธฐ๋ณธ์ ์ธ ์„ค์ •์€ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌ,
ํ•„์š”ํ•œ ๋งŒํผ์˜ ๊ธฐ๋Šฅ๋งŒ ์žฌ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ

 

โ–ท์Šคํ”„๋ง๋ถ€ํŠธ์˜ ํ•ต์‹ฌ์š”์†Œ

  • โ‘ ์Šคํƒ€ํ„ฐ : ํŠน์ • ๋ชจ๋“ˆ ์‚ฌ์šฉ์‹œ ๊ด€๋ จ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ๋ฌถ์–ด์„œ ๋งˆ์น˜ ํŒจํ‚ค์ง€์ฒ˜๋Ÿผ ์ œ๊ณต.
  • โ‘ก์ž๋™์„ค์ • : ๋นˆ ์„ค์ •์„ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌํ•จ
  • โ‘ข์•ก์ถ”์—์ดํ„ฐ : ์Šคํ”„๋ง๋ถ€ํŠธ๋กœ ๊ฐœ๋ฐœ๋œ ์‹œ์Šคํ…œ์„ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•จ

โ€‹

โ–ถ์˜์กด์„ฑ๊ด€๋ฆฌ

  • ์Šคํƒ€ํ„ฐ์˜ ์›๋ฆฌ : (๋ฉ”์ด๋ธ)์Šคํƒ€ํ„ฐ์— ์žˆ๋Š” POMํŒŒ์ผ์˜ ์ƒ์† ๊ตฌ์กฐ. <paremt>์—˜๋ฆฌ๋จผํŠธ ์‚ฌ์šฉ.
  • ํ”„๋กœํผํ‹ฐ ์žฌ์ •์˜
  • ๋ฉ”์ด๋ธ(pom.xml)
<dependencies>
    <!-- ๋‹ค๋ฅธ ์˜์กด์„ฑ๋“ค -->
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.5.4</version> <!-- ์Šคํ”„๋ง ๋ถ€ํŠธ ์Šคํƒ€ํ„ฐ์˜ ๋ฒ„์ „ ์ง€์ • -->
    </dependency>
    
    <!-- ๋‹ค๋ฅธ ์˜์กด์„ฑ๋“ค -->
</dependencies>
  • ๊ทธ๋ ˆ์ด๋“ค(build.gradle)
dependencies {
    // ๋‹ค๋ฅธ ์˜์กด์„ฑ๋“ค
    
    implementation 'org.springframework.boot:spring-boot-starter-web:2.5.4' // ์Šคํ”„๋ง ๋ถ€ํŠธ ์Šคํƒ€ํ„ฐ์˜ ๋ฒ„์ „ ์ง€์ •
    
    // ๋‹ค๋ฅธ ์˜์กด์„ฑ๋“ค
}

 

 

 

 

โ–ถ์ž๋™์„ค์ •

โ–ท์ž๋™์„ค์ •์ด๋ž€

  • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ํ•œ๋‹ค๊ณ  ๋ฐ”๋กœ ๋ชจ๋“ˆ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹˜. โ‘ ์Šคํ”„๋ง ์„ค์ • ํŒŒ์ผ์— ๋นˆ ๋“ฑ๋ก โ‘ก์˜์กด์„ฑ ์ฃผ์ž…๋„ ์ ์ ˆํžˆ ์ฒ˜๋ฆฌํ•ด์•ผํ•จ => ์ž๋™์œผ๋กœ ํ•ด์ฃผ๋Š” ๊ฒƒ
  • ๋ณต์žกํ•œ ์„ค์ •๋“ค์„ ๋Œ€์‹  ํ•ด์ฃผ๋Š” ๊ฒƒ : ๋ฉ”์ธํด๋ž˜์Šค ์œ„์— ์„ค์ •๋œ @SpringBootApplication
  • public @interface SpringBootApplication
@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 : ๊ธฐ์กด์— ํ™˜๊ฒฝ์„ค์ • ๋นˆ ํด๋ž˜์Šค๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด
  • @EnableAutoConfiguration : ์ž๋™์„ค์ • ๊ด€๋ จ ์• ๋„ˆํ…Œ์ด์…˜. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ด€๋ จ ๊ฐ์ฒด๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆฌ๋Š” ์ž‘์—….
  • @ComponentScan : ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ดˆ๊ธฐํ™” ๊ด€๋ จ ์• ๋„ˆํ…Œ์ด์…˜. ์ปจํŠธ๋กค๋Ÿฌ ๊ฐ์ฒด๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆผ.

(@Configuration, @Repository, @Service, @Controler, @RestController๊ฐ€ ๋ถ™์€ ๊ฐ์ฒด๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฎ๊น€)

โ€‹โ€‹

โ–ท์Šคํ”„๋ง๋นˆ ์• ๋„ˆํ…Œ์ด์…˜ ์ ์šฉ์ˆœ์„œ

  • โ‘  @ComponentScan(์‚ฌ์šฉ์ž ๋“ฑ๋ก ๋นˆ)
  • โ–ผ
  • โ‘ก @EnableAutoConfiguration(์ž๋™์„ค์ •์— ์˜ํ•œ ๋นˆ)

โ€‹โ€‹

โ–ท์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ด€๋ จ ์• ๋„ˆํ…Œ์ด์…˜

  • public class WebMvcAutoConfiguration
@Configuration(proxyBeanMethods = false)
@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 {...}
  • @Configuration : ์ด ํด๋ž˜์Šค๊ฐ€ ์Šคํ”„๋ฆฐ ๋นˆ ์„ค์ • ํด๋ž˜์Šค์ž„์„ ์˜๋ฏธํ•จ. @ComponentScan์ด ์ฒ˜๋ฆฌ๋  ๋•Œ ์ž์‹ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ด ํด๋ž˜์Šค์— @Bean์œผ๋กœ ์„ค์ •๋œ ๋ชจ๋“  ๋นˆ๋“ค๋„ ์ดˆ๊ธฐํ™”๋จ
  • @ConditionalOnWebApplication : ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํƒ€์ž…์ด ์–ด๋–ป๊ฒŒ ์„ค์ •๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•จ

(type : NONE/REACTIVE/SERVLET)

  • @ConditionalOnClass : (์กฐ๊ฑด)ํด๋ž˜์ŠคํŒจ์Šค์— ํŠน์ •ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•  ๋•Œ, ํ˜„์žฌ ์„ค์ • ํด๋ž˜์Šค๋ฅผ ์ ์šฉ
  • @ConditionalOnMissingBean : (์กฐ๊ฑด) ํŠน์ •ํด๋ž˜์Šค ๊ฐ์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ์—†์„ ๋•Œ, ํ˜„์žฌ ์„ค์ • ํด๋ž˜์Šค๋ฅผ ์ ์šฉ
  • @AutoConfigureOrder : ์ž๋™์„ค์ • ํด๋ž˜์Šค๋“ค์˜ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ง€์ •ํ• ๋•Œ ์‚ฌ์šฉํ•จ
  • @AutoConfigureAfter : ํ˜„์žฌ์˜ ์ž๋™์„ค์ • ํด๋ž˜์Šค๊ฐ€ ๋‹ค๋ฅธ ์ž๋™์„ค์ •ํด๋ž˜์Šค ๋‹ค์Œ์— ์ ์šฉ๋˜๋„๋ก ์ง€์ •

โ€‹โ€‹

โ–ท์ž๋™์„ค์ • ์žฌ์ •์˜

  • ๋ฉ”๋ชจ๋ฆฌ์— ๊ฐ™์€ ํƒ€์ž…์˜ ๋นˆ์ด ๋‘ ๊ฐœ๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์ถฉ๋Œ์ด ๋ฐœ์ƒ → ์ด์ „์— ์ƒ์„ฑ๋œ ๋นˆ์„ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•œ ๋นˆ์ด ๋ฎ์–ด์“ธ ์ˆ˜ ์žˆ๋„๋ก ์„ค์ • ํŒŒ์ผ ์ˆ˜์ •
spring.main.allow-bean-definition-overriding=false (๊ธฐ๋ณธ๊ฐ’)
spring.main.allow-bean-definition-overriding=true (๋ฎ์–ด์“ฐ๊ธฐ)
  • ๊ทธ๋Ÿฌ๋‚˜ ์ž๋™์„ค์ • ๋นˆ์ด ์ ์šฉ๋จ. ์ด์œ  : ์Šคํ”„๋ง๋ถ€ํŠธ์˜ ๋นˆ ๋“ฑ๋ก ์ˆœ์„œ ๋•Œ๋ฌธ.
  • ์Šคํ”„๋ง๋ถ€ํŠธ์˜ ๋นˆ ๋“ฑ๋ก ์ˆœ์„œ โ‘  @ComponentScan (์‚ฌ์šฉ์ž ๋“ฑ๋ก ๋นˆ) → โ‘ก @EnableAutoConfiguration (์ž๋™์„ค์ •์— ์˜ํ•œ ๋นˆ)
  • ์ž๋™์„ค์ • ๋นˆ์ด ์•„๋‹Œ ์ƒˆ๋กญ๊ฒŒ ์ •์˜ํ•œ ๋นˆ์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ → @Conditional ์‚ฌ์šฉํ•˜๊ธฐ(if๋ฌธ)
  • @ConditionalOnMissingBean : ๋“ฑ๋กํ•˜๋ ค๋Š” ๋นˆ์ด ๋ฉ”๋ชจ๋ฆฌ์— ์—†๋Š” ๊ฒฝ์šฐ์—๋งŒ ํ˜„์žฌ์˜ ๋นˆ ๋“ฑ๋ก์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ•จ. @EnableAutoConfiguration์ด ๋™์ž‘ํ•˜๋Š” ์‹œ์ ์—๋Š” ์ด๋ฏธ ๋“ฑ๋ก๋œ ๋นˆ์„ ์‚ฌ์šฉํ•˜๊ณ , ์ƒˆ๋กญ๊ฒŒ ๋นˆ์„ ์ƒ์„ฑํ•˜์ง€ ์•Š์Œ.

โ€‹