$npx -y skills add github/awesome-copilot --skill kotlin-springbootGet best practices for developing applications with Spring Boot and Kotlin.
| 1 | # Spring Boot with Kotlin Best Practices |
| 2 | |
| 3 | Your goal is to help me write high-quality, idiomatic Spring Boot applications using Kotlin. |
| 4 | |
| 5 | ## Project Setup & Structure |
| 6 | |
| 7 | - **Build Tool:** Use Maven (`pom.xml`) or Gradle (`build.gradle`) with the Kotlin plugins (`kotlin-maven-plugin` or `org.jetbrains.kotlin.jvm`). |
| 8 | - **Kotlin Plugins:** For JPA, enable the `kotlin-jpa` plugin to automatically make entity classes `open` without boilerplate. |
| 9 | - **Starters:** Use Spring Boot starters (e.g., `spring-boot-starter-web`, `spring-boot-starter-data-jpa`) as usual. |
| 10 | - **Package Structure:** Organize code by feature/domain (e.g., `com.example.app.order`, `com.example.app.user`) rather than by layer. |
| 11 | |
| 12 | ## Dependency Injection & Components |
| 13 | |
| 14 | - **Primary Constructors:** Always use the primary constructor for required dependency injection. It's the most idiomatic and concise approach in Kotlin. |
| 15 | - **Immutability:** Declare dependencies as `private val` in the primary constructor. Prefer `val` over `var` everywhere to promote immutability. |
| 16 | - **Component Stereotypes:** Use `@Service`, `@Repository`, and `@RestController` annotations just as you would in Java. |
| 17 | |
| 18 | ## Configuration |
| 19 | |
| 20 | - **Externalized Configuration:** Use `application.yml` for its readability and hierarchical structure. |
| 21 | - **Type-Safe Properties:** Use `@ConfigurationProperties` with `data class` to create immutable, type-safe configuration objects. |
| 22 | - **Profiles:** Use Spring Profiles (`application-dev.yml`, `application-prod.yml`) to manage environment-specific configurations. |
| 23 | - **Secrets Management:** Never hardcode secrets. Use environment variables or a dedicated secret management tool like HashiCorp Vault or AWS Secrets Manager. |
| 24 | |
| 25 | ## Web Layer (Controllers) |
| 26 | |
| 27 | - **RESTful APIs:** Design clear and consistent RESTful endpoints. |
| 28 | - **Data Classes for DTOs:** Use Kotlin `data class` for all DTOs. This provides `equals()`, `hashCode()`, `toString()`, and `copy()` for free and promotes immutability. |
| 29 | - **Validation:** Use Java Bean Validation (JSR 380) with annotations (`@Valid`, `@NotNull`, `@Size`) on your DTO data classes. |
| 30 | - **Error Handling:** Implement a global exception handler using `@ControllerAdvice` and `@ExceptionHandler` for consistent error responses. |
| 31 | |
| 32 | ## Service Layer |
| 33 | |
| 34 | - **Business Logic:** Encapsulate business logic within `@Service` classes. |
| 35 | - **Statelessness:** Services should be stateless. |
| 36 | - **Transaction Management:** Use `@Transactional` on service methods. In Kotlin, this can be applied to class or function level. |
| 37 | |
| 38 | ## Data Layer (Repositories) |
| 39 | |
| 40 | - **JPA Entities:** Define entities as classes. Remember they must be `open`. It's highly recommended to use the `kotlin-jpa` compiler plugin to handle this automatically. |
| 41 | - **Null Safety:** Leverage Kotlin's null-safety (`?`) to clearly define which entity fields are optional or required at the type level. |
| 42 | - **Spring Data JPA:** Use Spring Data JPA repositories by extending `JpaRepository` or `CrudRepository`. |
| 43 | - **Coroutines:** For reactive applications, leverage Spring Boot's support for Kotlin Coroutines in the data layer. |
| 44 | |
| 45 | ## Logging |
| 46 | |
| 47 | - **Companion Object Logger:** The idiomatic way to declare a logger is in a companion object. |
| 48 | ```kotlin |
| 49 | companion object { |
| 50 | private val logger = LoggerFactory.getLogger(MyClass::class.java) |
| 51 | } |
| 52 | ``` |
| 53 | - **Parameterized Logging:** Use parameterized messages (`logger.info("Processing user {}...", userId)`) for performance and clarity. |
| 54 | |
| 55 | ## Testing |
| 56 | |
| 57 | - **JUnit 5:** JUnit 5 is the default and works seamlessly with Kotlin. |
| 58 | - **Idiomatic Testing Libraries:** For more fluent and idiomatic tests, consider using **Kotest** for assertions and **MockK** for mocking. They are designed for Kotlin and offer a more expressive syntax. |
| 59 | - **Test Slices:** Use test slice annotations like `@WebMvcTest` or `@DataJpaTest` to test specific parts of the application. |
| 60 | - **Testcontainers:** Use Testcontainers for reliable integration tests with real databases, message brokers, etc. |
| 61 | |
| 62 | ## Coroutines & Asynchronous Programming |
| 63 | |
| 64 | - **`suspend` functions:** For non-blocking asynchronous code, use `suspend` functions in your controllers and services. Spring Boot has excellent support for coroutines. |
| 65 | - **Structured Concurrency:** Use `coroutineScope` or `supervisorScope` to manage the lifecycle of coroutines. |