SKILL.md
$2a
- Externalized Configuration: Use
application.ymlfor its readability and hierarchical structure.
- Type-Safe Properties: Use
@ConfigurationPropertieswithdata classto 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 classfor all DTOs. This providesequals(),hashCode(),toString(), andcopy()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
@ControllerAdviceand@ExceptionHandlerfor consistent error responses.
Service Layer
- Business Logic: Encapsulate business logic within
@Serviceclasses.
- Statelessness: Services should be stateless.
- Transaction Management: Use
@Transactionalon 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 thekotlin-jpacompiler 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
JpaRepositoryorCrudRepository.
- 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
@WebMvcTestor@DataJpaTestto test specific parts of the application.
- Testcontainers: Use Testcontainers for reliable integration tests with real databases, message brokers, etc.
Coroutines & Asynchronous Programming
- **
suspendfunctions:** For non-blocking asynchronous code, usesuspendfunctions in your controllers and services. Spring Boot has excellent support for coroutines.
- Structured Concurrency: Use
coroutineScopeorsupervisorScopeto manage the lifecycle of coroutines.