个人网站鉴赏,建设厅副厅长,企业网站源码挣钱吗,一个完整的个人网站创建Order项目实现Clean Hexagonal架构
前言
在上一节中#xff0c;讲到了Clean Hexagonal架构的理论部分#xff0c;并且通过图形解释了从MVC架构到清洁架构到演变。下面我们通过创建项目的方式来进一步理解Clean Hexagonal架构。
1.项目创建
1. 项目…创建Order项目实现Clean Hexagonal架构
前言
在上一节中讲到了Clean Hexagonal架构的理论部分并且通过图形解释了从MVC架构到清洁架构到演变。下面我们通过创建项目的方式来进一步理解Clean Hexagonal架构。
1.项目创建
1. 项目整体结构规划
项目采用 Maven 多模块架构父模块聚合子模块依赖层级清晰。 结构说明
order-system/ # 父项目聚合所有子模块
├── .git # Git 仓库目录
├── .gitignore # Git 忽略规则如 target/、.DS_Store 等
├── .idea/ # IntelliJ IDEA 配置目录自动生成
├── order-service/ # 订单服务模块子模块
│ ├── order-application/ # 应用层接口、DTO、服务入口
│ ├── order-container/ # 容器配置Spring Boot 启动类
│ ├── order-dataccess/ # 数据访问层DAO、JPA/MyBatis 实现
│ ├── order-domain/ # 领域层领域模型、领域服务
│ ├── order-message/ # 消息处理MQ 消费者/生产者
│ └── pom.xml # 子模块的 pom 文件
└── pom.xml # 父模块的 pom 文件1.1 父项目order-system
父项目聚合了所有的子模块进行统一的依赖管理对应的pom文件如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.4.1/versionrelativePath //parentgroupIdcom.jackmouse/groupIdartifactIdorder-system/artifactIdversion1.0-SNAPSHOT/versionpackagingpom/packagingmodulesmoduleorder-service/module/modulespropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncodingmaven-compiler-plugin.version3.13.0/maven-compiler-plugin.version/propertiesdependencyManagementdependenciesdependencygroupIdcom.jackmouse/groupIdartifactIdorder-domian-core/artifactIdversion${project.version}/version/dependencydependencygroupIdcom.jackmouse/groupIdartifactIdorder-application-service/artifactIdversion${project.version}/version/dependencydependencygroupIdcom.jackmouse/groupIdartifactIdorder-application/artifactIdversion${project.version}/version/dependencydependencygroupIdcom.jackmouse/groupIdartifactIdorder-dataaccess/artifactIdversion${project.version}/version/dependencydependencygroupIdcom.jackmouse/groupIdartifactIdorder-message/artifactIdversion${project.version}/version/dependency/dependencies/dependencyManagementdependencies/dependenciesbuildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion${maven-compiler-plugin.version}/versionconfigurationrelease17/release/configuration/plugin/plugins/build/project1.1 order-container容器/启动模块 作用 包含 Spring Boot 的主启动类Application.java是项目的入口。负责整合所有子模块的依赖如 order-application、order-dataccess 等。配置全局属性如数据源、消息队列连接、Web 端口等。 示例代码 SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}pom文件 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.jackmouse/groupIdartifactIdorder-service/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdorder-container/artifactIdpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdcom.jackmouse/groupIdartifactIdorder-domian-core/artifactId/dependencydependencygroupIdcom.jackmouse/groupIdartifactIdorder-application-service/artifactId/dependencydependencygroupIdcom.jackmouse/groupIdartifactIdorder-application/artifactId/dependencydependencygroupIdcom.jackmouse/groupIdartifactIdorder-message/artifactId/dependency/dependencies
/project1.2 order-application应用层 作用 调用领域层order-domain。处理输入输出如接收 HTTP 请求返回 DTO 对象。 pom文件 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.jackmouse/groupIdartifactIdorder-service/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdorder-application/artifactIdpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdcom.jackmouse/groupIdartifactIdorder-application-service/artifactId/dependency/dependencies/project1.3 order-domain领域层 作用 核心业务逻辑和领域模型如 Order 实体、OrderStatus 值对象。定义领域服务如校验订单规则的 OrderValidator。声明仓储接口如 OrderRepository但不实现具体逻辑。 可以将domain层在细化为service层和core层service层实现具体的业务逻辑和外部接口的定义core层定义实体和值对象。 关键文件 Order.java领域实体 public class Order {private Long id;private String orderNumber;private BigDecimal totalAmount;public void cancel() {// 领域逻辑校验订单是否可取消if (this.status OrderStatus.SHIPPED) {throw new IllegalStateException(已发货订单不可取消);}this.status OrderStatus.CANCELLED;}
}pom文件 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.jackmouse/groupIdartifactIdorder-service/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdorder-domain/artifactIdpackagingpom/packagingmodulesmoduleorder-domian-core/modulemoduleorder-application-service/module/modulespropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties/project1.4 order-dataccess数据访问层 作用 实现领域层定义的仓储接口如 OrderRepositoryImpl。集成 ORM 框架如 JPA、MyBatis操作数据库。处理数据持久化细节如分页查询、事务管理。 关键文件 OrderRepositoryImpl.java仓储实现 Repository
public class OrderRepositoryImpl implements OrderRepository {Autowiredprivate JdbcTemplate jdbcTemplate;Overridepublic void save(Order order) {String sql INSERT INTO orders (...) VALUES (...);jdbcTemplate.update(sql, ...);}
}pom文件 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.jackmouse/groupIdartifactIdorder-service/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdorder-dataaccess/artifactIdpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdcom.jackmouse/groupIdartifactIdorder-application-service/artifactId/dependency/dependencies/project1.5 order-message消息处理模块 作用 处理异步消息如订单创建后发送 Kafka 事件。定义消息生产者如 OrderEventPublisher和消费者如 PaymentEventListener。解耦系统间通信支持事件驱动架构。 关键文件 OrderEventPublisher.java消息生产者 Component
public class OrderEventPublisher {Autowiredprivate KafkaTemplateString, OrderEvent kafkaTemplate;public void publishOrderCreated(Order order) {OrderEvent event new OrderEvent(order);kafkaTemplate.send(order-topic, event);}
}pom文件 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.jackmouse/groupIdartifactIdorder-service/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdorder-message/artifactIdpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdcom.jackmouse/groupIdartifactIdorder-application-service/artifactId/dependency/dependencies/project2.生成依赖图
使用depgraph-maven-plugin插件可以生成项目的依赖关系。
首先要在系统安装graphvizhttps://www.graphviz.org/ mac使用brew安装过程中可能会因为系统版本高导致报错 使用–build-from-source命令用从源代码安装如
brew install --build-from-source graphviz安装完成后在项目的根目录执行
mvn com.github.ferstl:depgraph-maven-plugin:aggregate -DcreateImagetrue -DreduceEdgesfalse -Dscopecompile -Dincludescom.jackmouse*:*com.jackmouse为你自己的包路径执行成功后 项目的target目录下会生成png文件 可以看到domain层不依赖于其他任何外部部组件所有外部服务如数据库、消息队列、外部服务都通过接口和适配器与业务逻辑交互。核心业务逻辑保持独立易于测试和替换使得系统具有更好的扩展性和灵活性。