Skip to content
java-springboot logo

Spring Boot Best Practices

java-springboot

Comprehensive best practices guide for building production-ready Spring Boot applications.

github/awesome-copilot20.9Kinstalls39.7KstarsDocumentation

SKILL.md

Full skill instructions

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 (pom.xml) or Gradle (build.gradle) for dependency management.
  • Starters: Use Spring Boot starters (e.g., spring-boot-starter-web, spring-boot-starter-data-jpa) to simplify dependency management.
  • Package Structure: Organize code by feature/​domain (e.g., com.example.app.order, com.example.app.user) rather than by layer (e.g., 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, @Repository, and @Controller/@RestController annotations appropriately to define beans.

Configuration

  • Externalized Configuration: Use application.yml (or application.properties) for configuration. YAML is often preferred for its readability and hierarchical structure.
  • Type-Safe Properties: Use @ConfigurationProperties to bind configuration to strongly-typed Java objects.
  • Profiles: Use Spring Profiles (application-dev.yml, application-prod.yml) to manage environment-specific configurations.
  • 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, @Size) on DTOs to validate request payloads.
  • Error Handling: Implement a global exception handler using @ControllerAdvice and @ExceptionHandler to provide consistent error responses.

Service Layer

  • Business Logic: Encapsulate all business logic within @Service classes.
  • Statelessness: Services should be stateless.
  • Transaction Management: Use @Transactional on service methods to manage database transactions declaratively. Apply it at the most granular level necessary.

Data Layer (Repositories)

  • Spring Data JPA: Use Spring Data JPA repositories by extending JpaRepository or CrudRepository for standard database operations.
  • Custom Queries: For complex queries, use @Query or the JPA Criteria API.
  • 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 (logger.info("Processing user {}...", userId);) instead of string concatenation to improve performance.

Testing

  • Unit Tests: Write unit tests for services and components using JUnit 5 and a mocking framework like Mockito.
  • Integration Tests: Use @SpringBootTest for integration tests that load the Spring application context.
  • Test Slices: Use test slice annotations like @WebMvcTest (for controllers) or @DataJpaTest (for repositories) to test specific parts of the application in isolation.
  • 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.

More Documentation skills

lark-wiki logo
Documentation

lark-wiki

Manage Lark Wiki spaces, members, nodes, and document organization hierarchies.

17.5K 457.5K
View

Discover and call native Lark OpenAPI endpoints from official documentation when existing skills fall short.

17.5K 451K
View

Postgres performance optimization and best practices from Supabase. Use this skill when writing, reviewing, or optimizing Postgres queries, schema designs, or database configurations.

2.7K 431.1K
View
writing-plans logo
Documentation

writing-plans

Comprehensive implementation plans for multi-step tasks, breaking down specs into bite-sized, testable steps.

295.4K 268K
View
writing-skills logo
Documentation

writing-skills

Test-driven documentation for creating reusable agent techniques, patterns, and reference guides.

295.4K 205.7K
View

Guide for setting up and using Firebase Authentication. Use this skill when the user's app requires user sign-in, user management, or secure data access using auth rules.

462 163.8K
View

Builds and deploys Firebase SQL Connect (aka Firebase Data Connect) backends with PostgreSQL securely. Use when designing schemas with tables and relations, writing authorized queries and mutations, configuring real-time data updates, or generating type-safe SDKs. Use when you need a relational database with Firebase, or when the user mentions SQL Connect or Data Connect.

462 158.3K
View

Guide for implementing smooth, native-feeling animations using React's View Transition API (`<ViewTransition>` component, `addTransitionType`, and CSS view transition pseudo-elements). Use this skill whenever the user wants to add page transitions, animate route changes, create shared element animations, animate enter/exit of components, animate list reorder, implement directional (forward/back) navigation animations, or integrate view transitions in Next.js. Also use when the user mentions view transitions, `startViewTransition`, `ViewTransition`, transition types, or asks about animating between UI states in React without third-party animation libraries.

31.9K 143K
View

README-first repository scanner that extracts commands and classifies reproduction candidates without executing them.

497 139.6K
View

Content & writing AI tools

Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View
Easy Peasy AI logo
Content & writing

Easy Peasy AI

Streamline Your Content Creation with Easy Peasy AI.

Paid
View
L
Content & writing

LensGo

social creators, marketers and small teams producing visual assets

Freemium
View
L
Content & writing

Leonardo AI

Create AI art, images, and videos with prompts

Freemium
View
L
Content & writing

Luma Dream Machine

Creative teams producing multi-asset campaigns

Freemium
View
xpression camera logo
Content & writing

xpression camera

Instantly transform into anyone on video chats and streams.

Freemium
View
C
Content & writing

Caption My Photos

A tool to automate personalized captions generation for photos.

Paid
View
TextFX logo
Content & writing

TextFX

AI-powered tools to spark creative writing.

Free
View