๐ 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์,
@Configuration@EnableAutoConfiguration@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ํ๋์๋ ๋ํ์ ์ผ๋ก ์๋์ ์์กด์ฑ๋ค์ด ํฌํจ๋๋ค.
- Spring MVC
- @RestController, @RequestMapping, @GetMapping ๋ฑ ์ด๋ ธํ ์ด์ ์ฌ์ฉ ๊ฐ๋ฅ
- Jackson (JSON)
- ๊ฐ์ฒด โ JSON ์๋ ์ง๋ ฌํ/์ญ์ง๋ ฌํ
- ์ปจํธ๋กค๋ฌ์์ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ฉด ์๋์ผ๋ก JSON ๋ณํ
- @RequestBody, @ResponseBody ๊ธฐ๋ฐ ๋์
- Tomcat (๋ด์ฅ)
- 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์์คํฌ๋ฆฝํธ์ฒ๋ผ ๋์ํ๋ฏ๋ก ๋น ๋ฅธ ๋น๋๊ฐ ๊ฐ๋ฅํ๋ค.