跳到正文
Skillpedia
API 与服务器(后端)40k · #7265

kotlin-springboot

它能做什么

这份指南讲解如何按照最佳实践,使用 Kotlin 和 Spring Boot 构建应用。从项目初始化到依赖注入、错误处理等代码细节,都有涉及。

适用场景

  • ✓开始一个 Kotlin 和 Spring Boot 的新项目时。
  • ✓重构现有项目、提升代码质量时。
  • ✓不确定如何实现某个具体功能时。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/kotlin-springboot/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/kotlin-springboot/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我的 Kotlin Spring Boot 项目应该如何配置,才能以地道的 Kotlin 方式使用依赖注入?”

每周 5 个新技能,1 分钟读完。

工作原理

AI 会根据文档内容,建议编写代码的最佳方式。它重点利用 Kotlin 的特性,比如主构造函数和数据类(data class),让代码更简洁高效。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

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

  • Externalized Configuration: Use application.yml for its readability and hierarchical structure.
  • Type-Safe Properties: Use @ConfigurationProperties with data class to create immutable, type-safe configuration objects.
  • Profiles: Use Spring Profiles (application-dev.yml, application-prod.yml) to manage environment-specific configurations.
  • Secrets Management: Never hardcode secrets. Use environment variables or a dedicated secret management tool like HashiCorp Vault or AWS Secrets Manager.

Web Layer (Controllers)

  • RESTful APIs: Design clear and consistent RESTful endpoints.
  • Data Classes for DTOs: Use Kotlin data class for all DTOs. This provides equals(), hashCode(), toString(), and copy() for free and promotes immutability.
  • Validation: Use Java Bean Validation (JSR 380) with annotations (@Valid, @NotNull, @Size) on your DTO data classes.
  • Error Handling: Implement a global exception handler using @ControllerAdvice and @ExceptionHandler for consistent error responses.

Service Layer

  • Business Logic: Encapsulate business logic within @Service classes.
  • Statelessness: Services should be stateless.
  • Transaction Management: Use @Transactional on service methods. In Kotlin, this can be applied to class or function level.

Data Layer (Repositories)

  • 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.
  • Null Safety: Leverage Kotlin's null-safety (?) to clearly define which entity fields are optional or required at the type level.
  • Spring Data JPA: Use Spring Data JPA repositories by extending JpaRepository or CrudRepository.
  • Coroutines: For reactive applications, leverage Spring Boot's support for Kotlin Coroutines in the data layer.

Logging

  • Companion Object Logger: The idiomatic way to declare a logger is in a companion object.
    companion object {
        private val logger = LoggerFactory.getLogger(MyClass::class.java)
    }
    
  • Parameterized Logging: Use parameterized messages (logger.info("Processing user {}...", userId)) for performance and clarity.

Testing

  • JUnit 5: JUnit 5 is the default and works seamlessly with Kotlin.
  • 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.
  • Test Slices: Use test slice annotations like @WebMvcTest or @DataJpaTest to test specific parts of the application.
  • Testcontainers: Use Testcontainers for reliable integration tests with real databases, message brokers, etc.

Coroutines & Asynchronous Programming

  • suspend functions: For non-blocking asynchronous code, use suspend functions in your controllers and services. Spring Boot has excellent support for coroutines.
  • Structured Concurrency: Use coroutineScope or supervisorScope to manage the lifecycle of coroutines.
常见问题

什么是 Spring Boot 的“starters”?

它们是预先配置好的依赖包,能让你轻松为项目添加常用功能,比如 Web 支持或数据库访问。

为什么用 `application.yml` 而不用 `.properties`?

YAML 格式可读性更好,并且能以更清晰的层级结构来组织配置。

“不可变性”(immutability)是什么意思?

指一个值一旦设定就不能再被修改。这有助于避免复杂程序中出现错误。

详情

作者的原始描述

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

repo: github/awesome-copilot

热度

GitHub 星标
40k
排名
#7265
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能