[Spring] Spring ๐Ÿ†š Spring Boot

Sangho Hanยท2025๋…„ 4์›” 23์ผ
4

๐ŸŒฑ Spring

๋ชฉ๋ก ๋ณด๊ธฐ
7/13
post-thumbnail

๐Ÿ“Œ Spring Boot์˜ ๋ฐฐ๊ฒฝ

Spring์ด๋ผ๋Š” ์šฐ์ˆ˜ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•˜๋Š”๋ฐ, ์™œ Spring Boot๊ฐ€ ํƒ„์ƒํ–ˆ์„๊นŒ?

Spring์€ ์œ ์—ฐํ•˜๊ณ  ํ™•์žฅ์„ฑ ์žˆ๋Š” ๊ตฌ์กฐ ๋•๋ถ„์— ๋งŽ์ด ์‚ฌ์šฉ๋˜์—ˆ์ง€๋งŒ, ์ดˆ๊ธฐ ์„ค์ •์ด ๋ณต์žกํ•˜๊ณ  ๊ท€์ฐฎ์€ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

Spring์˜ ๋ฌธ์ œ์ 

1. ์„ค์ •ํ•  ๊ฒƒ๋“ค์ด ๋งŽ๋‹ค.

  • applicationContext.xml, dispatcher-servlet.xml, ๊ฐ์ข… bean ์„ค์ • ๋“ฑ
  • ํ•˜๋‚˜์˜ ๊ธฐ๋Šฅ์„ ์“ฐ๊ธฐ ์œ„ํ•ด ์ˆ˜์‹ญ ์ค„์˜ XML + ๊ด€๋ จ ํด๋ž˜์Šค ์ž‘์„ฑ

2. ๋นŒ๋“œ์™€ ๋ฐฐํฌ๊ฐ€ ๋ฒˆ๊ฑฐ๋กญ๋‹ค.

  • WAR ํŒŒ์ผ๋กœ ๋นŒ๋“œํ•˜๊ณ , ํ†ฐ์บฃ์— ์˜ฌ๋ฆฌ๊ณ  ์žฌ์‹œ์ž‘ํ•˜๋Š” ๋“ฑ์˜ ๊ณผ์ •

3. ์˜์กด์„ฑ ์„ค์ •์ด ๋ณต์žกํ•˜๋‹ค.

  • Maven์—์„œ ์–ด๋–ค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์–ด๋–ค ๋ฒ„์ „์œผ๋กœ ์จ์•ผ ํ• ์ง€ ๊ฐ์„ ์žก๊ธฐ ์–ด๋ ค์›€

4. ๋น ๋ฅธ ์‹คํ—˜์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค.

  • ์ž‘์€ API ํ•˜๋‚˜๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•ด๋„ ํด๋” ๊ตฌ์กฐ & ์„ค์ • & ๋ฉ”์ธ ํด๋ž˜์Šค ๋“ฑ ์ง„์ž…์žฅ๋ฒฝ์ด ๋†’์Œ

Spring Boot == Starter

Spring Boot๋Š” ๊ธฐ๋ณธ ์„ค์ •๊ณผ ๊ตฌ์กฐ๋ฅผ ์ž๋™์œผ๋กœ ๊ตฌ์„ฑํ•ด์ฃผ๋Š” ์Šคํƒ€ํ„ฐ์ด๋‹ค.
์ฆ‰, ์ดˆ๋ณด์ž๋„ ๋ฐ”๋กœ ์ƒ์‚ฐ์„ฑ์„ ๋‚ผ ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“  Spring ๊ธฐ๋ฐ˜์˜ ํ™•์žฅ ํ”„๋ ˆ์ž„์›Œํฌ์ธ ๊ฒƒ์ด๋‹ค.

Spring์ด ๋ถ€ํ’ˆ์ด ์ž˜ ์ •๋ฆฌ๋˜์–ด ์žˆ๋Š” ์ž๋™์ฐจ ๊ณต์žฅ์ด๋ผ๋ฉด, Spring Boot๋Š” ์‹œ๋™๋งŒ ๊ฑธ๋ฉด ๋ฐ”๋กœ ๋‹ฌ๋ฆด ์ˆ˜ ์žˆ๋Š” ์ž๋™์ฐจ ํ‚คํŠธ์™€ ์œ ์‚ฌํ•˜๋‹ค.


๐Ÿ“Œ Spring Boot์˜ ํ•ต์‹ฌ ๊ธฐ๋Šฅ

๊ธฐ์กด Spring์ด ๊ฐ€์ง„ ๋ณต์žกํ•จ์„ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹๋“ค๋กœ ํ•ด์†Œํ•ด์ค€๋‹ค.

1. ๋‚ด์žฅ ํ†ฐ์บฃ ์ง€์› (Embedded Tomcat)

  • ๋ณ„๋„๋กœ ํ†ฐ์บฃ์— ๋ฐฐํฌํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • ๋ฉ”์ธ ๋ฉ”์„œ๋“œ๋งŒ ์‹คํ–‰ํ•˜๋ฉด ๋ฐ”๋กœ ์›น ์„œ๋ฒ„๋ฅผ ๊ตฌ๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.

Spring (๊ธฐ์กด) : WAR ํŒŒ์ผ์„ ๋นŒ๋“œํ•ด์„œ ํ†ฐ์บฃ์— ์˜ฌ๋ ค์„œ ์‹คํ–‰
Spring Boot : ํ†ฐ์บฃ์ด ๋‚ด์žฅ๋˜์–ด ์žˆ์–ด์„œ main() ๋ฉ”์„œ๋“œ๋งŒ ์‹คํ–‰ํ•˜๋ฉด ๋ฐ”๋กœ ์‹คํ–‰๋จ

ํ†ฐ์บฃ(Tomcat)?

  • ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ์ด์ž ์›น ์„œ๋ฒ„์ด๋‹ค.
  • ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ๋“ค์–ด์˜ค๋Š” HTTP ์š”์ฒญ์„ ๋ฐ›์•„์„œ, ์„œ๋ธ”๋ฆฟ ๊ธฐ๋ฐ˜์˜ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ์ง์„ ์‹คํ–‰ํ•˜๊ณ  ์‘๋‹ต์„ ๋Œ๋ ค์ฃผ๋Š” ์—ญํ• ์ด๋‹ค.

2. Starter ์˜์กด์„ฑ

  • spring-boot-starter-web, spring-boot-starter-data-jpa ๋“ฑ ..
  • ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ•œ ๋ฒˆ์— ๊ฐ€์ ธ์˜ค๋Š” ์˜์กด์„ฑ ๋ฌถ์Œ์ด ์กด์žฌํ•œ๋‹ค.

3. ์ž๋™ ์„ค์ • (Auto Configuration)

  • @SpringBootApplication ์•ˆ์— @EnableAutoConfiguration์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.
  • ์ƒํ™ฉ์— ๋”ฐ๋ผ ์ž๋™์œผ๋กœ Bean์„ ๋“ฑ๋กํ•˜๊ณ  ์„ค์ •ํ•ด ์ค€๋‹ค.

4. application.yaml or application.properties

  • XML ์„ค์ • ๋Œ€์‹  ๊ฐ„๋‹จํ•˜๊ณ  ๋ช…์‹œ์ ์ธ ์„ค์ • ํŒŒ์ผ์ด ์กด์žฌํ•œ๋‹ค.

5. ๊ฐœ๋ฐœ ํŽธ์˜ ๊ธฐ๋Šฅ

  • devtools, actuator, test starter ๋“ฑ ..
  • ๋น ๋ฅธ ๊ฐœ๋ฐœ๊ณผ ์ง„๋‹จ์„ ์ง€์›ํ•œ๋‹ค.

๐Ÿ“Œ Spring (Boot ์ด์ „)์—์„œ์˜ ์›น ์„ค์ • ํ๋ฆ„

Spring MVC๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ณดํ†ต, web.xml + Java Config (๋˜๋Š” XML)์„ ์กฐํ•ฉํ•ด์„œ DispatcherServlet, ComponentScan, ViewResolver ๋“ฑ๋“ฑ์„ ์ „๋ถ€ ์ˆ˜๋™์œผ๋กœ ์„ค์ •ํ•ด์•ผ๋งŒ ํ–ˆ๋‹ค.

์˜ˆ์‹œ

web.xml: DispatcherServlet ๋“ฑ๋ก

<web-app>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

servlet-context.xml: ์„ค์ • ํŒŒ์ผ

<context:component-scan base-package="com.example.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/views/" />
  <property name="suffix" value=".jsp" />
</bean>

Controller

@Controller
@RequestMapping("/hello")
public class HelloController {

    @GetMapping
    public String hello(Model model) {
        model.addAttribute("data", "Hello!");
        return "hello"; // /WEB-INF/views/hello.jsp ๋กœ ์ด๋™
    }
}
  • ์–ด๋…ธํ…Œ์ด์…˜์€ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์ž‘๋™ํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์„ค์ •์€ ์ „๋ถ€ ์ˆ˜๋™์œผ๋กœ ์ž‘์—…๋œ๋‹ค.

๐Ÿ“Œ Spring Boot์—์„œ์˜ ์›น ์„ค์ • ํ๋ฆ„

1. ๋ฉ”์ธ ํด๋ž˜์Šค

@SpringBootApplication // ์ž๋™ ์„ค์ • + ์ปดํฌ๋„ŒํŠธ ์Šค์บ” + ์„ค์ • ํด๋ž˜์Šค ๋“ฑ๋ก
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

@SpringBootApplication์€,
1) @Configuration
2) @EnableAutoConfiguration
3) @ComponentScan
๋ฅผ ํ•ฉํ•œ ๊ฒƒ์ด๋‹ค.

1) @Configuration

  • ํ•ด๋‹น ํด๋ž˜์Šค๋Š” ์„ค์ • ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„๋‹ค.
  • Spring์˜ ์„ค์ • ์ •๋ณด, ์ฆ‰ ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋กํ•  Bean ์ •์˜๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.
@Configuration
public class AppConfig {
    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}

2) @EnableAutoConfiguration

  • Spring Boot๊ฐ€ ์ž๋™ ์„ค์ •์„ ํ•˜๋„๋ก ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„๋‹ค.
  • ์Šคํ”„๋ง ๋ถ€ํŠธ์˜ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์ด๋‹ค.
  • ํด๋ž˜์ŠคํŒจ์Šค์— ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ, ํ•„์š”ํ•œ ์„ค์ •์„ ์ž๋™์œผ๋กœ ๊ตฌ์„ฑํ•œ๋‹ค.

3) @ComponentScan

  • ํ˜„์žฌ ํŒจํ‚ค์ง€์™€ ๊ทธ ํ•˜์œ„ ํŒจํ‚ค์ง€๋ฅผ ์Šค์บ”ํ•ด์„œ Bean ๋“ฑ๋กํ•ด ์ฃผ์„ธ์š”. ๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„๋‹ค.
  • @Component, @Service, @Repository, @Controller ๋“ฑ์„ ์ž๋™์œผ๋กœ ์ฐพ์•„์„œ ๋“ฑ๋กํ•œ๋‹ค.
  • ์Šคํ”„๋ง์˜ ์ž๋™ ๋นˆ ๋“ฑ๋ก ๋งค์ปค๋‹ˆ์ฆ˜์„ ์ž‘๋™์‹œํ‚ค๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

2. ์ปจํŠธ๋กค๋Ÿฌ

@RestController
@RequestMapping("/hello")
public class HelloController {

    @GetMapping
    public String hello() {
        return "Hello, Spring Boot!";
    }
}
  • @RestController : ์‘๋‹ต์„ ์ž๋™์œผ๋กœ JSON์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด ์ค€๋‹ค.
  • @RequestMapping + @GetMapping : URL์„ ๋งคํ•‘ํ•œ๋‹ค.
  • View ์—†์ด ๋ฐ์ดํ„ฐ๋งŒ ์ฃผ๊ณ ๋ฐ›๋Š” REST ์Šคํƒ€์ผ์„ ๊ธฐ๋ณธ ์ง€์›ํ•œ๋‹ค.

3. ์˜์กด์„ฑ ์„ค์ •

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
}
  • spring-boot-starter-web ํ•˜๋‚˜์—๋Š” ๋Œ€ํ‘œ์ ์œผ๋กœ ์•„๋ž˜์˜ ์˜์กด์„ฑ๋“ค์ด ํฌํ•จ๋œ๋‹ค.

1) Spring MVC

  • @RestController, @RequestMapping, @GetMapping ๋“ฑ ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

2) Jackson (JSON)

  • ๊ฐ์ฒด โ†” JSON ์ž๋™ ์ง๋ ฌํ™”/์—ญ์ง๋ ฌํ™”
  • ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์ž๋™์œผ๋กœ JSON ๋ณ€ํ™˜
  • @RequestBody, @ResponseBody ๊ธฐ๋ฐ˜ ๋™์ž‘

3) Tomcat (๋‚ด์žฅ)

4) Validation

  • @Valid, @NotNull, @Email, @Min, @Max ๋“ฑ ์ง€์›
  • ์ปจํŠธ๋กค๋Ÿฌ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ”์ธ๋”ฉ ์‹œ ์ž๋™ ๊ฒ€์ฆ

4. ์„ค์ • ํŒŒ์ผ

server:
  port: 8081

spring:
  application:
    name: myapp
  • XML ๋Œ€์‹  YAML ๋˜๋Š” properties๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค์ • ๊ฐ€๋Šฅํ•˜๋‹ค.
  • @Value, @ConfigurationProperties ๋“ฑ์œผ๋กœ ์‰ฝ๊ฒŒ ์ฃผ์ž…์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿ“Œ Spring Boot์—์„œ gradle์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

Spring Boot์—์„œ๋„ Maven ๋นŒ๋“œ ๋ฐฉ์‹์„ ๋ฌผ๋ก  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋Œ€๋ถ€๋ถ„ Gralde์„ ์„ ํ˜ธํ•œ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ?

1. ๊ฐ„๊ฒฐํ•œ DSL ๊ธฐ๋ฐ˜ ์„ค์ • ๋ฐฉ์‹

DSL : Domain Specific Language (๋„๋ฉ”์ธ ํŠนํ™” ์–ธ์–ด)

  • Groovy๋Š” ์ž๋ฐ” ๊ธฐ๋ฐ˜์˜ ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ด๋‹ค.
  • Gradle DSL (Groovy DSL)์€ Gradle์ด Groovy ๋ฌธ๋ฒ•์„ ์ด์šฉํ•ด์„œ ๋งŒ๋“  ์„ค์ •์šฉ ๋ฌธ๋ฒ•์ด๋‹ค.

์ฆ‰, Gradle์€ Groovy(๋˜๋Š” Kotlin) ๊ธฐ๋ฐ˜์˜ DSL์„ ์‚ฌ์šฉํ•˜์—ฌ
๋นŒ๋“œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งˆ์น˜ ์ฝ”๋”ฉํ•˜๋“ฏ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

์•„๋ž˜์—์„œ Maven๊ณผ Gradle ๋ฐฉ์‹์„ ๋น„๊ตํ•ด ๋ณด์ž.

Maven

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Gradle (Groovy DSL)

implementation 'org.springframework.boot:spring-boot-starter-web'

2. ๋น ๋ฅธ ๋นŒ๋“œ ์†๋„

Gradle์€ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ธฐ์ˆ ์  ์ด์œ ๋กœ Maven๋ณด๋‹ค ๋น ๋ฅธ ๋นŒ๋“œ ์†๋„๋ฅผ ์ง€์›ํ•œ๋‹ค.

ํ•ญ๋ชฉ์„ค๋ช…
์บ์‹œ(Cache)์ด์ „ ๋นŒ๋“œ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•ด๋‘๊ณ  ๊ฐ™์€ ์ž‘์—…์€ ๋‹ค์‹œ ํ•˜์ง€ ์•Š์Œ
์ฆ๋ถ„ ๋นŒ๋“œ(Incremental Build)๋ณ€๊ฒฝ๋œ ํŒŒ์ผ๋งŒ ๋นŒ๋“œํ•˜์—ฌ ์ „์ฒด ๋นŒ๋“œ ์‹œ๊ฐ„์„ ๋‹จ์ถ•
๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ(Parallel Build)๋ชจ๋“ˆ/์ž‘์—… ๋‹จ์œ„๋กœ ์—ฌ๋Ÿฌ ์ž‘์—…์„ ๋™์‹œ์— ์ˆ˜ํ–‰
๋ฐ๋ชฌ(Daemon) ํ”„๋กœ์„ธ์ŠคJVM์„ ๊ณ„์† ๋„์›Œ๋‘ฌ์„œ ๋งค๋ฒˆ JVM ๋ถ€ํŒ… ์‹œ๊ฐ„์„ ์ค„์ž„

Maven์€ XML ๊ธฐ๋ฐ˜ ์ •์  ๊ตฌ์„ฑ์ด๊ธฐ์— ๋™์  ์ตœ์ ํ™”๊ฐ€ ์–ด๋ ต๋‹ค. ๋ฐ˜๋ฉด Gradle์€ ์Šคํฌ๋ฆฝํŠธ์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๋ฏ€๋กœ ๋น ๋ฅธ ๋นŒ๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

profile
์•ˆ๋…•ํ•˜์„ธ์š”. ๋น„์ฆˆ๋‹ˆ์Šค๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž, ํ•œ์ƒํ˜ธ์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€