Chuyển đến nội dung
Skillpedia
API và máy chủ (backend)40k · #7260

java-springboot

Skill này làm gì

Hướng dẫn này giúp bạn xây dựng ứng dụng Java bằng công nghệ Spring Boot, theo các thực hành được khuyến nghị. Nội dung bao gồm mọi thứ từ thiết lập ban đầu đến kiểm thử và bảo mật, giúp mã của bạn có chất lượng cao.

Khi nào nên dùng

  • ✓Khi bạn bắt đầu một dự án Spring Boot mới.
  • ✓Khi bạn tái cấu trúc mã Spring Boot hiện có.
  • ✓Khi bạn đang học phát triển với Spring Boot.

Liên kết skill

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

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/github/awesome-copilot/blob/main/skills/java-springboot/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Tôi nên tổ chức các thư mục của dự án Spring Boot như thế nào?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

AI đưa ra lời khuyên về cách thiết lập dự án, viết mã sạch và hiệu quả, quản lý cấu hình và dữ liệu, và đảm bảo ứng dụng Spring Boot của bạn an toàn và dễ kiểm thử.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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.
Câu hỏi thường gặp

“Starter” trong Spring Boot là gì?

Đây là các gói giúp đơn giản hóa việc thêm các chức năng thường dùng vào dự án, như hỗ trợ web hoặc truy cập cơ sở dữ liệu.

Vì sao nên dùng constructor injection?

Cách này giúp mã dễ kiểm thử hơn và thể hiện rõ ràng các phụ thuộc mà thành phần cần đến.

DTO là gì?

Đây là các đối tượng dùng để truyền dữ liệu giữa tầng API và client, tránh để lộ trực tiếp chi tiết nội bộ của cơ sở dữ liệu.

Chi tiết

Mô tả gốc của tác giả

Get best practices for developing applications with Spring Boot.

repo: github/awesome-copilot

Độ phổ biến

Sao GitHub
40k
Vị trí xếp hạng
#7260
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự