Mobile 69 installs

Spring Boot with Kotlin Best Practices

by github/awesome-copilot

Get best practices for developing applications with Spring Boot and Kotlin.

Skill content

Spring Boot development patterns and idioms tailored for Kotlin applications.

- Use primary constructors for dependency injection, data class for DTOs, and the kotlin-jpa plugin to automatically open entity classes without boilerplate.

- Organize code by feature/domain rather than layer; leverage Kotlin's null-safety to clearly define optional vs. required entity fields.

- Apply @ConfigurationProperties with data class for type-safe, immutable configuration; use application.yml and Spring Profiles for environment management.

- Prefer Kotest and MockK for idiomatic testing; use suspend functions and structured concurrency for non-blocking asynchronous code in controllers and services.

Spring Boot with Kotlin Best Practices

Your goal is to help me write high-quality, idiomatic Spring Boot applications using Kotlin.

Project Setup & Structure

- Build Tool: Use Maven (pom.xml) or Gradle (build.gradle) with the Kotlin plugins (kotlin-maven-plugin or org.jetbrains.kotlin.jvm).

- Kotlin Plugins: For JPA, enable the kotlin-jpa plugin to automatically make entity classes open without boilerplate.

- Starters: Use Spring Boot starters (e.g., spring-boot-starter-web, spring-boot-starter-data-jpa) as usual.

- Package Structure: Organize code by feature/domain (e.g., com.example.app.order, com.example.app.user) rather than by layer.

Dependency Injection & Components

- Primary Constructors: Always use the primary constructor for required dependency injection. It's the most idiomatic and concise approach in Kotlin.

- Immutability: Declare dependencies as private val in the primary constructor. Prefer val over var everywhere to promote immutability.

- Component Stereotypes: Use @Service, @Repository, and @RestController annotations just as you would in Java.

Configuration