Damnatiox
DOCUMENT / published

Layered、Package by Feature 与领域边界

Layered、Package by Feature 与领域边界 Freshness metadata last verified : 2026 08 24 version scope : stable architecture concepts source type : architecture literature + engineering synthesis stability : stable concept control

应用架构与模块化单体 2026/8/242 分钟阅读
# Java# Java 后端# 应用架构与模块化单体

Layered、Package by Feature 与领域边界

Freshness metadata

  • last_verified: 2026-08-24
  • version_scope: stable architecture concepts
  • source_type: architecture literature + engineering synthesis
  • stability: stable-concept

controller/service/repository 全局分层容易让一个业务变更跨越全仓库。Package by feature 以 orders, inventory, billing 聚合代码,再在模块内部使用 adapter/application/domain 分层。

Text
orders/ api/ # published API/events internal/ web/ application/ domain/ persistence/

Bounded Context 是模型/语言边界,不是“每个表一个服务”。Application Service 处理 use case、权限上下文和事务;Domain Service 处理不自然属于单一实体的领域规则;Repository boundary 只暴露领域需要的集合式接口。

验证依赖

  • 模块外只能调用公开 API;
  • domain 不依赖 Spring MVC/JPA annotations(是否完全纯净按成本决定);
  • 禁止跨模块直接访问 internal repository/table;
  • architecture test 或 Spring Modulith verification 检查 cycle 与暴露面。