
Starter μμ‘΄μ±μΌλ‘ λΉ λ₯Έ μμ π: Spring Bootλ μμ£Ό μ¬μ©λλ λΌμ΄λΈλ¬λ¦¬λ€μ 미리 λ¬Άμ΄ 'starter' ν¨ν€μ§λ‘ μ 곡ν©λλ€. μλ₯Ό λ€μ΄, Spring Web Starterλ μΉ μ ν리μΌμ΄μ μ νμν λΌμ΄λΈλ¬λ¦¬λ₯Ό ν¬ν¨ν΄ λ³λμ μ€μ μμ΄ λ°λ‘ μμν μ μμ΅λλ€.
μλ μ€μ μΌλ‘ μκ° μ μ½ π: Spring Bootκ° λ³΅μ‘ν μ€μ μ μλμΌλ‘ μ²λ¦¬ν΄ μ£ΌκΈ° λλ¬Έμ, κ°λ°μλ ν΅μ¬ λ‘μ§μ λ λ§μ μκ°μ μμ μ μμ΅λλ€.
λ 립μ μ€ν κ°λ₯ π: Spring Bootλ μΉ μ ν리μΌμ΄μ μλ²(WAS) μμ΄λ λ 립μ μΌλ‘ μ€νν μ μμ΄, WASμ μ’ μλμ§ μλ μμ λ‘μ΄ κ΅¬μΆμ΄ κ°λ₯ν©λλ€.
λ΄μ₯ WAS μ§μ π₯οΈ: Tomcat, Jetty λ± WASλ₯Ό λ΄μ₯νκ³ μμ΄ λ³λ μ€μΉ μμ΄ μ ν리μΌμ΄μ μ μ€νν μ μμ΅λλ€. μ΄λ₯Ό ν΅ν΄ κ°λ°κ³Ό λ°°ν¬ κ³Όμ μ΄ λμ± κ°νΈν΄μ§λλ€.
JAR νμΌλ‘ κ°νΈν λ°°ν¬ π¦: Spring Boot μ ν리μΌμ΄μ μ JAR νμΌλ‘ ν¨ν€μ§λμ΄, λ¨μ μ€νμΌλ‘λ ꡬλμ΄ κ°λ₯ν©λλ€. λ°°ν¬μ μ€νμ΄ μ¬μμ§λ κ°λ°κ³Ό μ΄μμ΄ νκ²° ν¨μ¨μ μ λλ€.
μμ‘΄μ± λͺ¨μ ν λ²μ! π¦: Spring Boot Starterλ νΉμ κΈ°λ₯μ μννλ λ° νμν λΌμ΄λΈλ¬λ¦¬λ€μ ν κ·Έλ£ΉμΌλ‘ λ¬Άμ΄μ μ 곡ν©λλ€. λλΆμ κ°λ°μλ νμν λΌμ΄λΈλ¬λ¦¬λ€μ λ°λ‘ μΆκ°ν νμ μμ΄ Starter νλλ‘ ν λ²μ μ€μ ν μ μμ΅λλ€.
μ€μ μ κ°νΈν βοΈ: Starterλ₯Ό μ¬μ©νλ©΄ 볡μ‘ν μ€μ μμ΄ μνλ κΈ°λ₯μ μ½κ² νλ‘μ νΈμ μΆκ°ν μ μμ΅λλ€. μλ₯Ό λ€μ΄, μΉ μ ν리μΌμ΄μ
κ΅¬μΆ μ spring-boot-starter-webμ μΆκ°νλ©΄ νμν ꡬμ±μ΄ μλμΌλ‘ μ€μ λ©λλ€.
μ§κ΄μ μΈ λ€μ΄λ° 컨벀μ
βοΈ: Starterμ μ΄λ¦μ spring-boot-starter-{μμ
μ ν} νμμ λ°λ¦
λλ€. μνλ μμ
μ νμ λ§μΆ° μ μ ν Starterλ₯Ό μ νν μ μμ΅λλ€. μλ₯Ό λ€μ΄, λ°μ΄ν°λ² μ΄μ€ μ°λμ μν΄ spring-boot-starter-data-jpaλ₯Ό, μΉ κ°λ°μ μν΄ spring-boot-starter-webμ μΆκ°νλ λ°©μμ΄μ£ !
<dependency> νκ·Έλ₯Ό ν΅ν΄ Maven μμ‘΄μ±μ μ€μ νλ λ°©μμ μ΄ν΄λ΄
μλ€. μ£Όμ μμλ λ€μκ³Ό κ°μ΅λλ€:spring-boot-starter-data-jpa: λ°μ΄ν°λ² μ΄μ€μ μ°λν΄ JPAλ₯Ό μ¬μ©ν μ μλλ‘ μ¬λ¬ λΌμ΄λΈλ¬λ¦¬λ₯Ό λ¬Άμ΄ μ 곡ν©λλ€.spring-boot-starter-web: μΉ μ ν리μΌμ΄μ
κ°λ°μ νμν λΌμ΄λΈλ¬λ¦¬λ€, Spring MVCμ λ΄μ₯ WASλ₯Ό ν¬ν¨ν΄ μΉ μλ²λ₯Ό μ½κ² ꡬμΆν μ μμ΅λλ€.spring-boot-starter-test: ν
μ€νΈ λΌμ΄λΈλ¬λ¦¬ λͺ¨μμΌλ‘, JUnitμ΄λ Mockito κ°μ ν
μ€νΈ λꡬλ₯Ό κ°νΈνκ² μ¬μ©ν μ μκ² ν΄μ€λλ€.spring-boot-starter-web π
spring-boot-starter-test π§ͺ
spring-boot-starter-validation β
spring-boot-starter-actuator π
spring-boot-starter-data-jpa ποΈ
νλ‘μ νΈ μ΄λ¦: HelloSpringBoot π
HelloSpringBootλ‘ λͺ
λͺ
νμ¬ μμ±ν©λλ€.λΉλ λꡬ: Maven βοΈ
ν¨ν€μ§ νμ: Jar π¦
Java λ²μ : 17 β
κ°λ° μΈμ΄: Java π
Group ID: com.ssafy π’
com.ssafyλ‘ μ€μ νμ΅λλ€.Artifact ID: hello π
helloλ‘ μ€μ νμ΅λλ€.ν¨ν€μ§ κ²½λ‘: com.ssafy.hello π
com.ssafy.hello ν¨ν€μ§ κ²½λ‘λ‘ μ€μ νμ΅λλ€.src/main/java π
HelloSpringBootApplication.java π
src/main/java λλ ν 리μ λ©μΈ ν΄λμ€μ
λλ€. @SpringBootApplication μ΄λ
Έν
μ΄μ
μ΄ λΆμ΄ μμΌλ©°, μ ν리μΌμ΄μ
μ μ§μ
μ μ΄ λλ λ©μΈ λ©μλκ° ν¬ν¨λμ΄ μμ΄ Spring Boot μ ν리μΌμ΄μ
μ΄ μ΄ ν΄λμ€μμ μμλ©λλ€.src/main/resources π
static λλ ν 리, templates λλ ν 리, κ·Έλ¦¬κ³ application.properties νμΌμ΄ μ£Όλ‘ μμΉν©λλ€.static π
templates π¨
application.properties βοΈ
src/main/webapp π
src/test/java π§ͺ: ν
μ€νΈ μ½λκ° μμΉνλ λλ ν 리λ‘, μ ν리μΌμ΄μ
κΈ°λ₯μ κ²μ¦νλ ν
μ€νΈ ν΄λμ€λ€μ΄ ν¬ν¨λ©λλ€.pom.xml π: Maven νλ‘μ νΈ μ€μ νμΌλ‘, μμ‘΄μ± κ΄λ¦¬μ λΉλ μ€μ μ μ μνλ ν΅μ¬ νμΌμ
λλ€.HelloSpringBootApplication.java: Spring Bootμ Entry Pointmain λ©μλκ° μμ΄ μ ν리μΌμ΄μ
μ μ€νν λ νΈμΆλ©λλ€.@SpringBootApplication μ΄λ
Έν
μ΄μ
: μ΄ μ΄λ
Έν
μ΄μ
μ μ¬λ¬ μ΄λ
Έν
μ΄μ
μ κ²°ν©ν λ©ν μ΄λ
Έν
μ΄μ
μΌλ‘, Spring Boot μ€μ μ ν¬ν¨ν©λλ€.@SpringBootApplicationμ κ΅¬μ± μμ@SpringBootConfiguration βοΈ
@Configurationκ³Ό μ μ¬νκ² μλνμ¬, μ΄ ν΄λμ€λ₯Ό Spring μ€μ νμΌλ‘ μΈμνκ² ν©λλ€.@EnableAutoConfiguration π
META-INF/spring.factories νμΌμ μ½μ΄ νμν μ€μ μ μλμΌλ‘ ꡬμ±ν©λλ€. μ΄λ₯Ό ν΅ν΄ λ°μ΄ν° μμ€, JPA, λ©μμ§ λ³ν λ± μ ν리μΌμ΄μ
μ νμν μ€μ μ΄ μλ μ²λ¦¬λ©λλ€.@ComponentScan π
@Componentκ° λΆμ ν΄λμ€λ₯Ό μ°Ύμ λΉ(Bean)μΌλ‘ λ±λ‘νκ² ν©λλ€.@Configuration, @Controller, @RestController, @Service, @Repository λ±μ΄ μ μΈλ ν΄λμ€λ₯Ό μλμΌλ‘ μ€μΊνμ¬ μ ν리μΌμ΄μ
컨ν
μ€νΈμ λ±λ‘ν©λλ€.TypeExcludeFilter.classλ AutoConfigurationExcludeFilter.class λ±μ μ¬μ©νμ¬ νΉμ λΉμ μ μΈν μ μμ΅λλ€.index.jsp)<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> languageλ μ¬μ© μΈμ΄λ₯Ό, contentTypeκ³Ό pageEncodingμ λ¬Έμμ μΈμ½λ©μ μ€μ ν©λλ€.<%@ taglib prefix="c" uri="jakarta.tags.core" %> cλ JSTL μ½μ΄ νκ·Έλ₯Ό μ¬μ©νκΈ° μν μ λμ¬μ
λλ€.<c:set var="root" value="${pageContext.request.contextPath}"/> root λ³μλ₯Ό 컨ν
μ€νΈ κ²½λ‘λ‘ μ€μ ν΄ JSPμμ μ½κ² κ²½λ‘λ₯Ό μ¬μ©ν μ μκ² ν©λλ€.msg1κ³Ό msg2 λ³μλ₯Ό μΆλ ₯νκ³ λ§ν¬λ₯Ό μ¬μ©νμ¬ λμ μ½ν
μΈ λ₯Ό νμν©λλ€.application.properties νμΌ μ€μ server.servlet.context-path=/myApp: μΉ μ ν리μΌμ΄μ
μ κΈ°λ³Έ κ²½λ‘λ₯Ό /myAppμΌλ‘ μ€μ ν©λλ€.server.port=80: μλ²μ ν¬νΈλ₯Ό 80λ²μΌλ‘ μ€μ ν©λλ€.spring.mvc.view.prefix=/WEB-INF/views/: JSP νμΌλ€μ΄ μμΉνλ κΈ°λ³Έ κ²½λ‘λ₯Ό μ€μ ν©λλ€.spring.mvc.view.suffix=.jsp: JSP νμΌμ νμ₯μλ₯Ό .jspλ‘ μ€μ νμ¬, μμ² μ νμΌ νμ₯μλ₯Ό μλ΅νκ³ μ κ·Όν μ μμ΅λλ€.pom.xml νμΌμ μμ‘΄μ± μΆκ°org.apache.tomcat.embed:tomcat-embed-jasper: JSP νμΌμ ν΄μνκΈ° μν΄ Tomcat Jasper μμ‘΄μ±μ μΆκ°ν©λλ€.jakarta.servlet.jsp.jstl λ° org.glassfish.web: JSTL νκ·Έ λΌμ΄λΈλ¬λ¦¬λ₯Ό μ¬μ©νκΈ° μν μμ‘΄μ±μ
λλ€. μ΄λ₯Ό ν΅ν΄ JSP νμΌμμ λ€μν JSTL μ½μ΄ νκ·Έλ₯Ό νμ©ν μ μμ΅λλ€.DataSourceκ° μ€μ λκ³ , spring-boot-starter-webμ μΆκ°νλ©΄ λ΄μ₯λ μΉ μλ²(Tomcat)κ° μλμΌλ‘ μ€μ λ©λλ€.@SpringBootApplicationμ΄ λΆμ ν΄λμ€μ νμ ν¨ν€μ§μμ μ»΄ν¬λνΈ(@Component, @Service, @Repository, @Controller λ±)λ₯Ό μλμΌλ‘ μ€μΊν΄ λΉμΌλ‘ λ±λ‘ν©λλ€.application.properties νμΌμ κΈ°λ³Έ κ²½λ‘src/main/resourcesμ μμΉν application.properties λλ application.yml νμΌμ κΈ°λ³Έ μ€μ νμΌλ‘ μ¬μ©ν©λλ€.spring.mvc.view.prefixμ spring.mvc.view.suffixμ κΈ°λ³Έ κ°μΌλ‘ /WEB-INF/ λλ ν 리μ .jsp νμ₯μκ° μ§μ λ©λλ€.spring-boot-starter-webμ μΆκ°νλ©΄ κΈ°λ³Έμ μΌλ‘ Tomcat λ΄μ₯ μλ²κ° μ€νλ©λλ€. λ³λμ WAS μ€μ μ΄ νμ μμ΄ μλ²κ° μλμΌλ‘ μ€νλ©λλ€.spring-boot-starter-data-jpa, spring-boot-starter-web λ±κ³Ό κ°μ Starter ν¨ν€μ§λ₯Ό ν΅ν΄ μμ£Ό μ¬μ©νλ λΌμ΄λΈλ¬λ¦¬λ₯Ό 미리 μ‘°ν©νμ¬ μ 곡λ©λλ€.