java-springboot
Original:🇺🇸 English
Translated
Get best practices for developing applications with Spring Boot.
9.0kinstalls
Sourcegithub/awesome-copilot
Added on
NPX Install
npx skill4agent add github/awesome-copilot java-springbootTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Spring Boot Best Practices
Your goal is to help me write high-quality Spring Boot applications by following established best practices.
Project Setup & Structure
- Build Tool: Use Maven () or Gradle (
pom.xml) for dependency management.build.gradle - Starters: Use Spring Boot starters (e.g., ,
spring-boot-starter-web) to simplify dependency management.spring-boot-starter-data-jpa - Package Structure: Organize code by feature/domain (e.g., ,
com.example.app.order) rather than by layer (e.g.,com.example.app.user,com.example.app.controller).com.example.app.service
Dependency Injection & Components
- Constructor Injection: Always use constructor-based injection for required dependencies. This makes components easier to test and dependencies explicit.
- Immutability: Declare dependency fields as .
private final - Component Stereotypes: Use ,
@Component,@Service, and@Repository/@Controllerannotations appropriately to define beans.@RestController
Configuration
- Externalized Configuration: Use (or
application.yml) for configuration. YAML is often preferred for its readability and hierarchical structure.application.properties - Type-Safe Properties: Use to bind configuration to strongly-typed Java objects.
@ConfigurationProperties - Profiles: Use Spring Profiles (,
application-dev.yml) to manage environment-specific configurations.application-prod.yml - Secrets Management: Do not 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.
- DTOs (Data Transfer Objects): Use DTOs to expose and consume data in the API layer. Do not expose JPA entities directly to the client.
- Validation: Use Java Bean Validation (JSR 380) with annotations (,
@Valid,@NotNull) on DTOs to validate request payloads.@Size - Error Handling: Implement a global exception handler using and
@ControllerAdviceto provide consistent error responses.@ExceptionHandler
Service Layer
- Business Logic: Encapsulate all business logic within classes.
@Service - Statelessness: Services should be stateless.
- Transaction Management: Use on service methods to manage database transactions declaratively. Apply it at the most granular level necessary.
@Transactional
Data Layer (Repositories)
- Spring Data JPA: Use Spring Data JPA repositories by extending or
JpaRepositoryfor standard database operations.CrudRepository - Custom Queries: For complex queries, use or the JPA Criteria API.
@Query - Projections: Use DTO projections to fetch only the necessary data from the database.
Logging
- SLF4J: Use the SLF4J API for logging.
- Logger Declaration:
private static final Logger logger = LoggerFactory.getLogger(MyClass.class); - Parameterized Logging: Use parameterized messages () instead of string concatenation to improve performance.
logger.info("Processing user {}...", userId);
Testing
- Unit Tests: Write unit tests for services and components using JUnit 5 and a mocking framework like Mockito.
- Integration Tests: Use for integration tests that load the Spring application context.
@SpringBootTest - Test Slices: Use test slice annotations like (for controllers) or
@WebMvcTest(for repositories) to test specific parts of the application in isolation.@DataJpaTest - Testcontainers: Consider using Testcontainers for reliable integration tests with real databases, message brokers, etc.
Security
- Spring Security: Use Spring Security for authentication and authorization.
- Password Encoding: Always encode passwords using a strong hashing algorithm like BCrypt.
- Input Sanitization: Prevent SQL injection by using Spring Data JPA or parameterized queries. Prevent Cross-Site Scripting (XSS) by properly encoding output.