kotlin-springboot

作者: github

专为Kotlin应用定制的Spring Boot开发模式与惯用法。使用主构造函数进行依赖注入,用数据类定义DTO,通过kotlin-jpa插件自动开放实体类而无需样板代码。按功能/领域而非层级组织代码;利用Kotlin的空安全特性清晰定义实体的可选字段与必填字段。结合数据类与@ConfigurationProperties实现类型安全、不可变的配置;使用application.yml和Spring Profiles...

npx skills add https://github.com/github/awesome-copilot --skill kotlin-springboot

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.

来自 github 的更多技能

console-rendering
github
在Go中使用基于结构体标签的控制台渲染系统的说明
official
acquire-codebase-knowledge
github
当用户明确要求映射、记录或熟悉现有代码库时使用此技能。触发词如“映射此代码库”、“记录…
official
acreadiness-assess
github
Run the AgentRC readiness assessment on the current repository and produce a static HTML dashboard at reports/index.html. Wraps `npx github:microsoft/agentrc…
official
acreadiness-generate-instructions
github
通过AgentRC指令命令生成定制化的AI代理指令文件。生成.github/copilot-instructions.md(默认,推荐用于VS Code中的Copilot…
official
acreadiness-policy
github
帮助用户选择、编写或应用AgentRC策略。策略通过禁用无关检查、覆盖影响/级别、设置…来定制就绪评分。
official
add-educational-comments
github
为代码文件添加教育性注释,将其转化为有效的学习资源。根据三个可配置的知识水平(初级、中级、高级)调整解释深度和语气。若未提供文件,自动请求文件,并附带编号列表以便快速选择。仅通过教育性注释将文件扩展最多125%(硬性限制:新增400行;超过1000行的文件限制为300行)。保留文件编码、缩进风格、语法正确性以及...
official
adobe-illustrator-scripting
github
使用ExtendScript(JavaScript/JSX)编写、调试和优化Adobe Illustrator自动化脚本。在创建或修改操作…的脚本时使用。
official
agent-governance
github
声明式策略、意图分类及审计追踪,用于控制AI代理工具访问与行为。可组合的治理策略定义允许/禁止的工具、内容过滤器、速率限制及审批要求——以配置而非代码形式存储。语义意图分类在执行工具前通过基于模式的信号检测危险提示(数据泄露、权限提升、提示注入)。工具级治理装饰器在函数层面强制执行策略...
official