1. Spring Boot란?
Spring Boot는 기존 Spring 프레임워크의 복잡한 설정을 간소화하고, 웹 애플리케이션을 빠르게 개발할 수 있도록 도와주는 도구입니다. "Just Run"이라는 철학을 바탕으로, 최소한의 설정으로 Spring 애플리케이션을 실행할 수 있게 해줍니다.
2. Spring vs Spring Boot
기존 Spring 프레임워크의 문제점
Spring 프레임워크는 강력하지만, 처음 시작할 때 다음과 같은 설정이 번거로울 수 있습니다:
- 복잡한 XML 설정
- 의존성 버전 관리의 어려움
- 서버 설정의 번거로움
예시) 기존 Spring 설정
<!-- 기존 Spring의 복잡한 XML 설정 예시 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.example.demo" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
</beans>
Spring Boot의 해결책
Spring Boot는 다음과 같은 방식으로 기존 스프링의 문제를 해결합니다:
- 자동 설정 (Auto-Configuration): 스프링 부트가 필요한 설정을 자동으로 구성합니다.
- Starter 의존성 관리: 필요한 라이브러리 그룹을 미리 정의하여 의존성 설정을 간소화합니다.
- 내장 서버 제공: 톰캣(Tomcat) 같은 서버가 내장되어 있어 별도의 서버 설정 없이도 애플리케이션을 실행할 수 있습니다.
예시) Spring Boot 설정
# Spring Boot의 간단한 설정 (application.yml)
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: password
3. Spring Boot의 주요 특징
3.1 자동 설정 (Auto Configuration)
Spring Boot는 @SpringBootApplication 어노테이션 하나로 대부분의 설정을 자동화합니다.
@SpringBootApplication // 자동 설정의 시작점
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3.2 Starter 의존성 관리
필요한 라이브러리들을 미리 정의된 그룹으로 제공합니다. 예를 들어, spring-boot-starter-web은 웹 애플리케이션 개발에 필요한 모든 라이브러리를 포함합니다.
dependencies {
// 웹 애플리케이션 개발에 필요한 모든 의존성 포함
implementation 'org.springframework.boot:spring-boot-starter-web'
// JPA 사용에 필요한 모든 의존성 포함
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// 테스트 관련 의존성
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
3.3 내장 서버
Spring Boot는 톰캣, 제티, 언더토우 같은 웹 서버를 애플리케이션에 내장하여 별도 서버 설정 없이도 애플리케이션을 바로 실행할 수 있습니다.
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}
이제 애플리케이션을 실행하고 http://localhost:8080/hello에 접속하면 "Hello, Spring Boot!"라는 응답을 확인할 수 있습니다.
3.4 프로파일 기반 설정
Spring Boot는 환경별로 다른 설정을 적용할 수 있는 프로파일 기능을 제공합니다. 예를 들어 개발, 테스트, 운영 환경에 따라 데이터베이스 설정을 다르게 구성할 수 있습니다.
# application.yml
spring:
profiles:
active: dev
--- # 개발 환경 설정
spring:
config:
activate:
on-profile: dev
datasource:
url: jdbc:h2:mem:testdb
--- # 운영 환경 설정
spring:
config:
activate:
on-profile: prod
datasource:
url: jdbc:mysql://production-db:3306/mydb
4. 실제 사용 예시
4.1 간단한 REST API 구현
Spring Boot를 사용하면 간단한 REST API도 빠르게 구현할 수 있습니다. 아래 예시는 사용자 정보를 관리하는 기본적인 API 예제입니다.
@RestController
@RequestMapping("/api/v1")
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/users")
public ResponseEntity<List<User>> getUsers() {
return ResponseEntity.ok(userService.findAll());
}
@PostMapping("/users")
public ResponseEntity<User> createUser(@RequestBody UserRequest request) {
User user = userService.create(request);
return ResponseEntity.status(HttpStatus.CREATED).body(user);
}
}
이 예제를 실행하면, GET /api/v1/users로 전체 사용자 목록을 가져오고, POST /api/v1/users로 새로운 사용자를 생성할 수 있습니다.
5. 정리
Spring Boot는 다음과 같은 이점을 제공합니다:
- 복잡한 설정 없이 빠른 개발 가능
- 자동화된 설정으로 생산성 향상
- 독립적으로 실행 가능한 애플리케이션 생성
- 통합된 의존성 관리로 버전 충돌 방지
Spring Boot의 등장으로 인해 스프링 애플리케이션 개발이 훨씬 쉬워졌으며, 이로 인해 현대적인 자바 애플리케이션 개발의 표준으로 자리 잡았습니다.
'SpringBoot' 카테고리의 다른 글
| Spring Boot: HTTP 메서드와 매핑 (1) | 2024.11.12 |
|---|---|
| Spring Boot: RESTful API란? (0) | 2024.11.11 |
| Spring Boot: 의존성 관리 - Starter 패키지 (0) | 2024.11.10 |
| Spring Boot: 의존성 관리 (2) | 2024.11.09 |
| 프로젝트의 기본 디렉토리 구조와 설정 파일 (0) | 2024.11.08 |