본문 바로가기
Spring Boot

[Kotlin Spring Boot 강의] 프로젝트 생성 방법

by 뷰티풀스택 2025. 1. 24.
반응형

코틀린 기반의 스프링부트 시작하기 

 

1. 프로젝트 생성하기

코틀린 스프링 부트가 처음이라면 아래와 같이 세팅하여 프로젝트를 생성하기를 권장한다. 참고로, Amazon Corretto는 프로덕션 레벨의 상용 서비스를 위해 아마존에서 오픈 JDK를 만든 것으로써, 수많은 상용 서비스들을 통해 검증되고 업데이트되고 있는점이 강점이라고 할 수 있겠다. 당연히, Linux/Windows/macOS를 지원하며 Kotlin에서도 Corretto를 권장한다. 버전은 가장 최신보다 한단계 아래의 LTS를 사용하면 dependancy 문제 없이 사용할 수 있다.

Kotlin Spring Boot 프로젝트 시작하기

 

Amazon Corretto 다운로드

검색하면 아래와 같은 주소로 들어가 클릭 몇번만 하면 쉽게 설치가 된다.

Downloads for Amazon Corretto 17 - Amazon Corretto 17

 

Downloads for Amazon Corretto 17 - Amazon Corretto 17

Thanks for letting us know this page needs work. We're sorry we let you down. If you've got a moment, please tell us how we can make the documentation better.

docs.aws.amazon.com

 

2. Main function

프로젝트를 생성하면 소스 디렉토리를 자동생성되며, main 함수는 DemoApplication.kt 파일에 존재하게 된다.

package com.example.demo

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}

 

3. Gradle Build File

루트에 보면 build.gradle.kts 라는 설정 파일이 존재한다. 이곳에 모든 환경 정보와 dependancy 라이브러리들이 정리되어 있다.

plugins {
    kotlin("jvm") version "1.9.25"
    kotlin("plugin.spring") version "1.9.25"
    id("org.springframework.boot") version "3.4.1"
    id("io.spring.dependency-management") version "1.1.7"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    runtimeOnly("com.h2database:h2")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

kotlin {
    compilerOptions {
        freeCompilerArgs.addAll("-Xjsr305=strict")
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

 

특히, kotlin(plugin.spring)은 코틀린 클래스들을 Spring Framework에서 사용될 수 있도록 관련 modifier 들을 클래스에 추가해주는 플러그인이다.

kotlin("plugin.spring") version "1.9.25"

 

그리고,  아래의 모듈은 어제 강의한 Kotlin data class들을 serialization 할 수 있도록 지원해주는 기능이다.

implementation("com.fasterxml.jackson.module:jackson-module-kotlin")

 

4. 소스 코드 이해

@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}

@SpringBootApplication annotation은 우리가 만든 클래스를 Spring Boot 프레임워크에 포함되도록 하여 auto configuration이나 component scan 등의 기능을 지원받도록 하기 위한 것이다.

main() 함수는  DemoApplication이 Spring Framework의 요소로써 등록시켰다면 main은 그 Spring Framework를 실행시키는 역할을 한다고 보면 되겠다.

 

다음 강의에서는 웹서비스를 바로 시작할 수 있는 Controller를 생성하는 방법에 대해서 알아보겠다.

반응형

댓글