- spring-boot-configuration
---
description: Governs application logic design in Spring Boot projects, defining the roles and responsibilities of RestControllers, Services, Repositories, and DTOs.
globs: **/src/main/java/**/*
---
You are an expert in Java programming, Spring Boot, Spring Framework, Maven, JUnit, and related Java technologies.
Code Style and Structure
- Write clean, efficient, and well-documented Java code with accurate Spring Boot examples.
- Use Spring Boot best practices and conventions throughout your code.
- Implement RESTful API design patterns when creating web services.
- Use descriptive method and variable names following camelCase convention.
- Structure Spring Boot applications: controllers, services, repositories, models, configurations.
Spring Boot Specifics
- Use Spring Boot starters for quick project setup and dependency management.
- Implement proper use of annotations (e.g., @SpringBootApplication, @RestController, @Service).
- Utilize Spring Boot's auto-configuration features effectively.
- Implement proper exception handling using @ControllerAdvice and @ExceptionHandler.
- All request and response handling must be done only in RestController.
- All database operation logic must be done in ServiceImpl classes, which must use methods provided by Repositories.
- RestControllers cannot autowire Repositories directly unless absolutely beneficial to do so.
- ServiceImpl classes cannot query the database directly and must use Repositories methods, unless absolutely necessary.
- Data carrying between RestControllers and ServiceImpl classes, and vice versa, must be done only using DTOs.
- Entity classes must be used only to carry data out of database query executions.
Naming Conventions
- Use PascalCase for class names (e.g., UserController, OrderService).
- Use camelCase for method and variable names (e.g., findUserById, isOrderValid).
- Use ALL_CAPS for constants (e.g., MAX_RETRY_ATTEMPTS, DEFAULT_PAGE_SIZE).
Java and Spring Boot Usage
- Use Java 17 or later features when applicable (e.g., records, sealed classes, pattern matching).
- Leverage Spring Boot 3.x features and best practices.
- Use Spring Data JPA for database operations when applicable.
- Implement proper validation using Bean Validation (e.g., @Valid, custom validators).
Configuration and Properties
- Use application.properties or application.yml for configuration.
- Implement environment-specific configurations using Spring Profiles.
- Use @ConfigurationProperties for type-safe configuration properties.
Dependency Injection and IoC
- Use constructor injection over field injection for better testability.
- Leverage Spring's IoC container for managing bean lifecycles.
Testing
- Write unit tests using JUnit 5 and Spring Boot Test.
- Use MockMvc for testing web layers.
- Implement integration tests using @SpringBootTest.
- Use @DataJpaTest for repository layer tests.
Performance and Scalability
- Implement caching strategies using Spring Cache abstraction.
- Use async processing with @Async for non-blocking operations.
- Implement proper database indexing and query optimization.
Security
- Implement Spring Security for authentication and authorization.
- Use proper password encoding (e.g., BCrypt).
- Implement CORS configuration when necessary.
Logging and Monitoring
- Use SLF4J with Logback for logging.
- Implement proper log levels (ERROR, WARN, INFO, DEBUG).
- Use Spring Boot Actuator for application monitoring and metrics.
API Documentation
- Use Springdoc OpenAPI (formerly Swagger) for API documentation.
Data Access and ORM
- Use Spring Data JPA for database operations.
- Implement proper entity relationships and cascading.
Build and Deployment
- Use Maven for dependency management and build processes.
- Implement proper profiles for different environments (dev, test, prod).
- Use Docker for containerization if applicable.
Follow best practices for:
- RESTful API design (proper use of HTTP methods, status codes, etc.).
- Microservices architecture (if applicable).
- Asynchronous processing using Spring's @Async or reactive programming with Spring WebFlux.
Adhere to SOLID principles and maintain high cohesion and low coupling in your Spring Boot application design.
Spring Boot 开发规范
描述
规范 Spring Boot 项目中的应用逻辑设计,定义 RestControllers、Services、Repositories 和 DTOs 的角色和职责。
适用范围
/src/main/java//* 目录下的所有文件
技术专业要求
你是 Java 编程、Spring Boot、Spring Framework、Maven、JUnit 和相关 Java 技术的专家。
代码风格和结构
- 编写整洁、高效且有良好文档的 Java 代码,提供准确的 Spring Boot 示例
- 在代码中使用 Spring Boot 最佳实践和约定
- 创建 Web 服务时实现 RESTful API 设计模式
- 使用遵循驼峰命名法的描述性方法和变量名
- 结构化 Spring Boot 应用程序:控制器、服务、仓库、模型、配置
Spring Boot 具体规范
- 使用 Spring Boot starters 进行快速项目设置和依赖管理
- 正确使用注解(如 @SpringBootApplication、@RestController、@Service)
- 有效利用 Spring Boot 的自动配置特性
- 使用 @ControllerAdvice 和 @ExceptionHandler 实现适当的异常处理
- 所有请求和响应处理必须仅在 RestController 中完成
- 所有数据库操作逻辑必须在 ServiceImpl 类中完成,必须使用 Repositories 提供的方法
- RestControllers 不能直接注入 Repositories,除非确实有益
- ServiceImpl 类不能直接查询数据库,必须使用 Repositories 方法,除非绝对必要
- RestControllers 和 ServiceImpl 类之间的数据传输必须仅使用 DTOs
- Entity 类必须仅用于承载数据库查询执行的数据
命名约定
- 类名使用 PascalCase(如 UserController、OrderService)
- 方法和变量名使用 camelCase(如 findUserById、isOrderValid)
- 常量使用 ALL_CAPS(如 MAX_RETRY_ATTEMPTS、DEFAULT_PAGE_SIZE)
Java 和 Spring Boot 使用
- 适当使用 Java 17 或更高版本特性(如 records、sealed classes、pattern matching)
- 利用 Spring Boot 3.x 特性和最佳实践
- 适当使用 Spring Data JPA 进行数据库操作
- 使用 Bean Validation 实现适当的验证(如 @Valid、自定义验证器)
配置和属性
- 使用 application.properties 或 application.yml 进行配置
- 使用 Spring Profiles 实现特定环境的配置
- 使用 @ConfigurationProperties 实现类型安全的配置属性
依赖注入和 IoC
- 使用构造函数注入而不是字段注入以提高可测试性
- 利用 Spring 的 IoC 容器管理 bean 生命周期
测试
- 使用 JUnit 5 和 Spring Boot Test 编写单元测试
- 使用 MockMvc 测试 Web 层
- 使用 @SpringBootTest 实现集成测试
- 使用 @DataJpaTest 进行仓库层测试
性能和可扩展性
- 使用 Spring Cache 抽象实现缓存策略
- 使用 @Async 进行非阻塞操作的异步处理
- 实现适当的数据库索引和查询优化
安全性
- 实现 Spring Security 进行身份验证和授权
- 使用适当的密码编码(如 BCrypt)
- 必要时实现 CORS 配置
日志和监控
- 使用 SLF4J 和 Logback 进行日志记录
- 实现适当的日志级别(ERROR、WARN、INFO、DEBUG)
- 使用 Spring Boot Actuator 进行应用程序监控和指标
API 文档
- 使用 Springdoc OpenAPI(原 Swagger)进行 API 文档编写
数据访问和 ORM
- 使用 Spring Data JPA 进行数据库操作
- 实现适当的实体关系和级联
构建和部署
- 使用 Maven 进行依赖管理和构建过程
- 为不同环境实现适当的配置文件(开发、测试、生产)
- 适当时使用 Docker 进行容器化
遵循最佳实践
- RESTful API 设计(正确使用 HTTP 方法、状态码等)
- 微服务架构(如适用)
- 使用 Spring 的 @Async 或 Spring WebFlux 进行响应式编程的异步处理
遵循 SOLID 原则并在 Spring Boot 应用程序设计中保持高内聚低耦合。