跳到正文
Skillpedia
API 与服务器(后端)40k · #7260

java-springboot

它能做什么

这份指南可以帮你使用 Spring Boot 技术构建 Java 应用,并遵循推荐的最佳实践。从项目初始化到测试和安全,它都有涵盖,帮助你写出高质量的代码。

适用场景

  • ✓开始一个新的 Spring Boot 项目时。
  • ✓重构现有的 Spring Boot 代码时。
  • ✓学习 Spring Boot 开发时。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/java-springboot/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/java-springboot/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我应该如何组织 Spring Boot 项目的文件夹?”

每周 5 个新技能,1 分钟读完。

工作原理

AI 会给出建议,告诉你如何搭建项目、编写简洁高效的代码、管理配置和数据,并确保你的 Spring Boot 应用既安全又便于测试。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

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.
常见问题

什么是 Spring Boot 的“starters”?

它们是一些打包好的依赖,能帮你更轻松地为项目添加常用功能,比如 Web 或数据访问。

为什么要使用构造器注入?

它能让代码更容易测试,也能清楚地看出组件需要哪些依赖。

什么是 DTO?

DTO 是在 API 层和客户端之间传递数据的对象,可以避免直接暴露数据库的内部细节。

详情

作者的原始描述

Get best practices for developing applications with Spring Boot.

repo: github/awesome-copilot

热度

GitHub 星标
40k
排名
#7260
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能