
Spring์ด๋ผ๋ ์ฐ์ํ ํ๋ ์์ํฌ๊ฐ ์ด๋ฏธ ์กด์ฌํ๋๋ฐ, ์ Spring Boot๊ฐ ํ์ํ์๊น?
Spring์ ์ ์ฐํ๊ณ ํ์ฅ์ฑ ์๋ ๊ตฌ์กฐ ๋๋ถ์ ๋ง์ด ์ฌ์ฉ๋์์ง๋ง, ์ด๊ธฐ ์ค์ ์ด ๋ณต์กํ๊ณ ๊ท์ฐฎ์ ๋ฌธ์ ๊ฐ ์์๋ค. ๋ํ์ ์ผ๋ก ์๋์ ๊ฐ๋ค.
1. ์ค์ ํ ๊ฒ๋ค์ด ๋ง๋ค.
- applicationContext.xml, dispatcher-servlet.xml, ๊ฐ์ข bean ์ค์ ๋ฑ
- ํ๋์ ๊ธฐ๋ฅ์ ์ฐ๊ธฐ ์ํด ์์ญ ์ค์ XML + ๊ด๋ จ ํด๋์ค ์์ฑ
2. ๋น๋์ ๋ฐฐํฌ๊ฐ ๋ฒ๊ฑฐ๋กญ๋ค.
- WAR ํ์ผ๋ก ๋น๋ํ๊ณ , ํฐ์บฃ์ ์ฌ๋ฆฌ๊ณ ์ฌ์์ํ๋ ๋ฑ์ ๊ณผ์
3. ์์กด์ฑ ์ค์ ์ด ๋ณต์กํ๋ค.
- Maven์์ ์ด๋ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด๋ค ๋ฒ์ ์ผ๋ก ์จ์ผ ํ ์ง ๊ฐ์ ์ก๊ธฐ ์ด๋ ค์
4. ๋น ๋ฅธ ์คํ์ด ๋ถ๊ฐํ๋ค.
- ์์ API ํ๋๋ฅผ ๋ง๋ค๋ ค๊ณ ํด๋ ํด๋ ๊ตฌ์กฐ & ์ค์ & ๋ฉ์ธ ํด๋์ค ๋ฑ ์ง์ ์ฅ๋ฒฝ์ด ๋์
Spring Boot๋ ๊ธฐ๋ณธ ์ค์ ๊ณผ ๊ตฌ์กฐ๋ฅผ ์๋์ผ๋ก ๊ตฌ์ฑํด์ฃผ๋ ์คํํฐ์ด๋ค.
์ฆ, ์ด๋ณด์๋ ๋ฐ๋ก ์์ฐ์ฑ์ ๋ผ ์ ์๋๋ก ๋ง๋ Spring ๊ธฐ๋ฐ์ ํ์ฅ ํ๋ ์์ํฌ์ธ ๊ฒ์ด๋ค.
Spring์ด ๋ถํ์ด ์ ์ ๋ฆฌ๋์ด ์๋ ์๋์ฐจ ๊ณต์ฅ์ด๋ผ๋ฉด, Spring Boot๋ ์๋๋ง ๊ฑธ๋ฉด ๋ฐ๋ก ๋ฌ๋ฆด ์ ์๋ ์๋์ฐจ ํคํธ์ ์ ์ฌํ๋ค.
๊ธฐ์กด Spring์ด ๊ฐ์ง ๋ณต์กํจ์ ์๋์ ๊ฐ์ ๋ฐฉ์๋ค๋ก ํด์ํด์ค๋ค.
Spring (๊ธฐ์กด) : WAR ํ์ผ์ ๋น๋ํด์ ํฐ์บฃ์ ์ฌ๋ ค์ ์คํ
Spring Boot : ํฐ์บฃ์ด ๋ด์ฅ๋์ด ์์ด์ main() ๋ฉ์๋๋ง ์คํํ๋ฉด ๋ฐ๋ก ์คํ๋จ
@SpringBootApplication ์์ @EnableAutoConfiguration์ด ํฌํจ๋์ด ์๋ค.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 ๋ก ์ด๋
}
}
@SpringBootApplication // ์๋ ์ค์ + ์ปดํฌ๋ํธ ์ค์บ + ์ค์ ํด๋์ค ๋ฑ๋ก
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
@SpringBootApplication์,
1)@Configuration
2)@EnableAutoConfiguration
3)@ComponentScan
๋ฅผ ํฉํ ๊ฒ์ด๋ค.
@Configurationํด๋น ํด๋์ค๋ ์ค์ ํด๋์ค์
๋๋ค. ๋ผ๋ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ค.@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
@EnableAutoConfigurationSpring Boot๊ฐ ์๋ ์ค์ ์ ํ๋๋ก ํ์ฉํฉ๋๋ค. ๋ผ๋ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ค.@ComponentScanํ์ฌ ํจํค์ง์ ๊ทธ ํ์ ํจํค์ง๋ฅผ ์ค์บํด์ Bean ๋ฑ๋กํด ์ฃผ์ธ์. ๋ผ๋ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ค.@Component, @Service, @Repository, @Controller ๋ฑ์ ์๋์ผ๋ก ์ฐพ์์ ๋ฑ๋กํ๋ค.@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping
public String hello() {
return "Hello, Spring Boot!";
}
}
@RestController : ์๋ต์ ์๋์ผ๋ก JSON์ผ๋ก ๋ฐํํด ์ค๋ค.@RequestMapping + @GetMapping : URL์ ๋งคํํ๋ค.REST ์คํ์ผ์ ๊ธฐ๋ณธ ์ง์ํ๋ค.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๋ฑ ์ง์- ์ปจํธ๋กค๋ฌ ํ๋ผ๋ฏธํฐ ๋ฐ์ธ๋ฉ ์ ์๋ ๊ฒ์ฆ
server:
port: 8081
spring:
application:
name: myapp
@Value, @ConfigurationProperties ๋ฑ์ผ๋ก ์ฝ๊ฒ ์ฃผ์
์ด ๊ฐ๋ฅํ๋ค.Spring Boot์์๋ Maven ๋น๋ ๋ฐฉ์์ ๋ฌผ๋ก ์ฌ์ฉํ ์ ์์ง๋ง, ๋๋ถ๋ถ Gralde์ ์ ํธํ๋ค. ๊ทธ ์ด์ ๋ ๋ฌด์์ผ๊น?
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'
Gradle์ ์๋์ ๊ฐ์ ๊ธฐ์ ์ ์ด์ ๋ก Maven๋ณด๋ค ๋น ๋ฅธ ๋น๋ ์๋๋ฅผ ์ง์ํ๋ค.
| ํญ๋ชฉ | ์ค๋ช |
|---|---|
| ์บ์(Cache) | ์ด์ ๋น๋ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํด๋๊ณ ๊ฐ์ ์์ ์ ๋ค์ ํ์ง ์์ |
| ์ฆ๋ถ ๋น๋(Incremental Build) | ๋ณ๊ฒฝ๋ ํ์ผ๋ง ๋น๋ํ์ฌ ์ ์ฒด ๋น๋ ์๊ฐ์ ๋จ์ถ |
| ๋ณ๋ ฌ ์ฒ๋ฆฌ(Parallel Build) | ๋ชจ๋/์์ ๋จ์๋ก ์ฌ๋ฌ ์์ ์ ๋์์ ์ํ |
| ๋ฐ๋ชฌ(Daemon) ํ๋ก์ธ์ค | JVM์ ๊ณ์ ๋์๋ฌ์ ๋งค๋ฒ JVM ๋ถํ ์๊ฐ์ ์ค์ |
Maven์
XML ๊ธฐ๋ฐ ์ ์ ๊ตฌ์ฑ์ด๊ธฐ์ ๋์ ์ต์ ํ๊ฐ ์ด๋ ต๋ค. ๋ฐ๋ฉด Gradle์์คํฌ๋ฆฝํธ์ฒ๋ผ ๋์ํ๋ฏ๋ก ๋น ๋ฅธ ๋น๋๊ฐ ๊ฐ๋ฅํ๋ค.