Paperless / rebuild
Course 2 28ef273

annotations.md

docs/annotations.md

Snapshot 28ef273Download original
1# Annotations23## Kotlin Idioms (No Lombok)45Lombok is not used in this project because Kotlin natively provides these capabilities:67- **Primary constructors:** Used for dependency injection in classes (replacing `@RequiredArgsConstructor`).8- **Data classes (`data class`):** Provide getters, setters (for `var`), `copy()`, `equals()`, `hashCode()`, and `toString()` natively without boilerplate.9- **Default and named arguments:** Replace builder patterns and `@Builder`. Construct objects directly using named parameters.10- **`model/` domain models:** Declared as Kotlin `data class` with `val` properties, and **never** import `jakarta.persistence.*`.11- **`entity/` classes:** Plain Kotlin classes carrying JPA annotations and nothing else — no business logic. The `kotlin-jpa` plugin generates the zero-argument constructor required by JPA.12- **Logging:** Use SLF4J `LoggerFactory.getLogger(...)` directly (replacing `@Slf4j`). See [`docs/logging.md`](logging.md).1314## Spring Annotations1516- `@RestController`: On web controllers, paired with HTTP method annotations at method level (`@GetMapping`, `@PostMapping`, `@PatchMapping`, `@DeleteMapping`).17- `@Service`: Placed on the feature implementation `<Feature>ServiceImpl`, which implements the plain `<Feature>Service` interface (the interface itself carries no annotations).18- `@Repository`: On `Jpa<Feature>Persistence` adapters, enabling persistence exception translation. Spring Data repositories in `persistence/repository/` stay plain interfaces extending `JpaRepository`; Spring Data generates their implementations. Domain persistence interfaces carry no Spring annotations.19- `@Component`: For generic Spring-managed beans; `@Configuration` for configuration classes.20- `@Autowired`: Constructor injection is used in production code via Kotlin primary constructors. Spring automatically resolves single-constructor beans without `@Autowired`. Field injection (`@Autowired lateinit var`) is reserved strictly for test classes.21- `@ConfigurationProperties`: Used when binding 3 or more related configuration properties. For fewer than 3 properties, individual `@Value` annotations are acceptable.22- `@Transactional`: Lives at the **class level on `@Service` implementations only**, so transaction management never leaks into controllers or repository interfaces. Use `@Transactional(readOnly = true)` for read paths and `@Transactional` for write paths.23- `@Validated`: Placed at class or method parameter level to trigger Bean Validation. Use `@RequestBody @Valid` on incoming write DTOs.24- `@PreAuthorize`: Placed at the controller layer when Spring Security enforces method-level authorization.2526## Dependency Hygiene2728- Keep dependencies strictly acyclic.29- Do not use `@Order` to paper over a dependency resolution problem — fix the underlying dependency design instead.30

Search chapters, reference and source filenames.