yoonix

[Spring Boot] SpringBootServletInitializer๋Š” ์–ธ์ œ, ์™œ ํ•„์š”ํ•œ๊ฐ€? (WAR ๋ฐฐํฌ์™€ web.xml์˜ ์ฐจ์ด) ๋ณธ๋ฌธ

๊ฐœ๋ฐœ/Spring Boot

[Spring Boot] SpringBootServletInitializer๋Š” ์–ธ์ œ, ์™œ ํ•„์š”ํ•œ๊ฐ€? (WAR ๋ฐฐํฌ์™€ web.xml์˜ ์ฐจ์ด)

yoonix 2025. 5. 21. 23:22

๐Ÿงฉ ๊ฐœ์š”

Spring Boot๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด์„œ SpringBootServletInitializer๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ ‘ํ•˜๊ฒŒ ๋๋‹ค.
๋‚˜๋Š” ์ „์ž์ •๋ถ€ํ”„๋ ˆ์ž„์›Œํฌ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•  ๋• web.xml์„ ์„ค์ •ํ•˜๊ณ  ํ†ฐ์บฃ์— WAR์„ ๋ฐฐํฌํ•˜๋Š” ๊ตฌ์กฐ์— ์ต์ˆ™ํ–ˆ๋‹ค.
๊ทธ๋ž˜์„œ Spring Boot์—์„œ๋Š” web.xml์ด ์—†๋‹ค๋Š” ๊ฒŒ ์ƒ์†Œํ–ˆ๊ณ ,
๋Œ€์ฒด ์ด ๊ตฌ์กฐ์—์„œ “๋ฉ”์ธ ๋ฉ”์„œ๋“œ ์—ญํ• ”์„ ๋ˆ„๊ฐ€ ํ•ด์ฃผ๋Š”์ง€,
WAR ๋ฐฐํฌ ์‹œ์—๋Š” ์–ด๋–ค ์„ค์ •์ด ํ•„์š”ํ•œ์ง€ ๊ฐœ๋…์ด ๋‹ค์†Œ ๋ชจํ˜ธํ•˜๊ฒŒ ๋А๊ปด์กŒ๋‹ค.

์ด ๊ธ€์€ ๊ทธ๋Ÿฐ ๊ถ๊ธˆ์ฆ์„ ์ •๋ฆฌํ•œ ์ผ์ข…์˜ ํšŒ๊ณ ์ด์ž,
SpringBootServletInitializer์˜ ํ•„์š”์„ฑ๊ณผ ์ž‘๋™ ๋ฐฉ์‹์„ JAR vs WAR ๋ฐฐํฌ ๊ตฌ์กฐ ์ฐจ์ด ์ค‘์‹ฌ์œผ๋กœ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค.

โœ… Spring Boot๋Š” ์™œ web.xml์ด ์—†์„๊นŒ?

์ „์ž์ •๋ถ€ํ”„๋ ˆ์ž„์›Œํฌ(MVC2 ๊ธฐ๋ฐ˜)์—์„œ๋Š” ํ†ฐ์บฃ ๊ฐ™์€ WAS์— .war ํŒŒ์ผ์„ ๋ฐฐํฌํ•˜๊ณ 
๊ทธ ์•ˆ์˜ web.xml์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฑธ ์ง€์ •ํ–ˆ๋‹ค.

  • ์‹œ์ž‘ ์„œ๋ธ”๋ฆฟ (DispatcherServlet ๋“ฑ)
  • ์ดˆ๊ธฐ ํŒŒ๋ผ๋ฏธํ„ฐ
  • ์ธ๋ฑ์Šค ํŒŒ์ผ (index.jsp ๋“ฑ)
<web-app>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
</web-app>
 

 

๐Ÿ’ก Spring Boot๋Š” ๋‚ด์žฅ ํ†ฐ์บฃ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์–ด์„œ, ์„œ๋ฒ„ ์„ค์ • ์ž์ฒด๋ฅผ ์ฝ”๋“œ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  main() ํ•จ์ˆ˜๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‹œ์ž‘์  ์—ญํ• ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ.

 

์ฆ‰, ํ†ฐ์บฃ๋„, ์„œ๋ธ”๋ฆฟ ๋“ฑ๋ก๋„ ๋‹ค ์ž๋™์ด๋‹ค.

 

โœ… ๊ทธ๋Ÿฐ๋ฐ ์™ธ๋ถ€ WAS์— WAR์„ ๋ฐฐํฌํ•  ๋•Œ๋Š”?

๋ฌธ์ œ๋Š” Spring Boot๋ฅผ WAR๋กœ ํŒจํ‚ค์ง•ํ•ด์„œ ์™ธ๋ถ€ ํ†ฐ์บฃ์— ๋ฐฐํฌํ•  ๊ฒฝ์šฐ๋‹ค.
์ด๋•Œ๋Š” ๋” ์ด์ƒ main()์„ ์ง์ ‘ ์‹คํ–‰ํ•  ์ˆ˜ ์—†๋‹ค.

์™ธ๋ถ€ ํ†ฐ์บฃ์€ web.xml ๋˜๋Š” Servlet API๋ฅผ ํ†ตํ•ด ์–ด๋–ค ํด๋ž˜์Šค๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹œ์ž‘ํ• ์ง€ ์•Œ์•„์•ผ ํ•œ๋‹ค.
๊ทธ๋ž˜์„œ ์ด๋•Œ ํ•„์š”ํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ ๐Ÿ‘‰ SpringBootServletInitializer

 

โœ… SpringBootServletInitializer๋ž€?

 

์™ธ๋ถ€ ํ†ฐ์บฃ์ด Spring Boot ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ถ€ํŒ…ํ•  ์ˆ˜ ์žˆ๋„๋ก
์ง„์ž…์ ์„ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ๋ธŒ๋ฆฟ์ง€ ํด๋ž˜์Šค

๋‚ด์žฅ ํ†ฐ์บฃ์ด ์—†๋Š” WAR ํ™˜๊ฒฝ์—์„œ๋Š”
์™ธ๋ถ€ ํ†ฐ์บฃ์ด web.xml์˜ Servlet์„ ์ฐธ๊ณ ํ•ด ์ง„์ž…์ ์„ ์ฐพ๋“ฏ์ด,
Spring Boot์—์„œ๋Š” SpringBootServletInitializer๊ฐ€ ์ง„์ž…์  ์—ญํ• ์„ ํ•˜๊ฒŒ ๋œ๋‹ค.

 

@SpringBootApplication
class MyApplication : SpringBootServletInitializer() {
    override fun configure(application: SpringApplicationBuilder): SpringApplicationBuilder {
        return application.sources(MyApplication::class.java)
    }
}

 

โœ… ์–ธ์ œ ํ•„์š”ํ•˜๊ณ , ์–ธ์ œ ํ•„์š” ์—†๋Š”๊ฐ€?

๋ฐฐํฌ ๋ฐฉ์‹ ํ†ฐ์บฃ ์ข…๋ฅ˜ ํ•„์š” ์—ฌ๋ถ€ ์„ค๋ช…
java -jar ๋‚ด์žฅ ํ†ฐ์บฃ โŒ ํ•„์š” ์—†์Œ main()์ด ์ง์ ‘ ์‹คํ–‰๋จ
.war ํŒŒ์ผ ์™ธ๋ถ€ ํ†ฐ์บฃ โœ… ํ•„์š”ํ•จ WAS๊ฐ€ Spring์„ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ดˆ๊ธฐํ™” ํด๋ž˜์Šค ํ•„์š”

 

โœ… ๋‚ด์žฅ ํ†ฐ์บฃ vs ์™ธ๋ถ€ ํ†ฐ์บฃ, ๋ญ๊ฐ€ ๋‹ค๋ฅธ๊ฐ€?

ํ•ญ๋ชฉ ๋‚ด์žฅ ํ†ฐ์บฃ ์™ธ๋ถ€ ํ†ฐ์บฃ
๋ฐฐํฌ jar ์‹คํ–‰ war ๋ณต์‚ฌ
์„ค์ • ์œ„์น˜ main() ๋‚ด๋ถ€ ์„ค์ • server.xml, webapps ํด๋” ๋“ฑ
Spring ์‹คํ–‰ ๋ฐฉ์‹ ์ง์ ‘ ์‹คํ–‰ WAS๊ฐ€ ์‹คํ–‰, Spring์€ ๋ถ€๋ฆ„๋ฐ›๋Š” ๊ตฌ์กฐ
SpringBootServletInitializer ํ•„์š” ์—ฌ๋ถ€ โŒ โœ…

 

โœ… ๊ทธ๋Ÿผ jar๋กœ ๋งŒ๋“ค๋ฉด ๋ฌด์กฐ๊ฑด ๋‚ด์žฅ ํ†ฐ์บฃ์ด ํฌํ•จ๋˜๋‚˜?

 

๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ, YES.

 

  • Spring Boot์—์„œ spring-boot-starter-web๋งŒ ์˜์กดํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋‚ด์žฅ ํ†ฐ์บฃ์ด ํฌํ•จ๋จ
  • ๋‹ค๋งŒ, providedRuntime("...tomcat...")์œผ๋กœ ์„ค์ •ํ•˜๊ฑฐ๋‚˜
  • Jetty/Nett ์„œ๋ฒ„๋กœ ๊ต์ฒดํ•˜๋ฉด ํ†ฐ์บฃ์€ ๋น ์ง
dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web") // ํ†ฐ์บฃ ํฌํ•จ
    // or
    // providedRuntime("org.springframework.boot:spring-boot-starter-tomcat") // WAR์šฉ
}

 

+ โš™๏ธ Gradle ์„ค์ • ์‹œ ์ฃผ์˜์ 

WAR ๋นŒ๋“œ๋ฅผ ํ•˜๋ ค๋ฉด build.gradle.kts์—์„œ ๋‹ค์Œ์ฒ˜๋Ÿผ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค:

plugins {
    id("war")
}

dependencies {
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
}

providedRuntime ์„ค์ •์„ ํ•ด์ค˜์•ผ ๋‚ด์žฅ ํ†ฐ์บฃ์„ JAR์— ํฌํ•จ์‹œํ‚ค์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.
WAR๋Š” ํ†ฐ์บฃ์ด ์™ธ๋ถ€์— ๋”ฐ๋กœ ์กด์žฌํ•˜๋ฏ€๋กœ, ๋‚ด์žฅ ํ†ฐ์บฃ์€ ๋นผ์ค˜์•ผ ํ•œ๋‹ค.