6 The running hexagon: From pieces to system
The chapter explains how the isolated parts of a hexagonal application become a working system. After building the domain, ports, handlers, and adapters separately, the missing piece is assembly: a runtime bridge that connects everything without letting framework concerns leak into the core. The result is a system that starts cleanly, serves requests, and keeps the architecture’s boundaries intact while still being practical to run.
At compile time, Maven modules enforce dependency direction so that inner modules do not depend on outer ones. The application core is split into domain, input ports, output ports, and application services, while adapters sit outside and depend only on the specific contracts they need. The assembly module is the only place that sees the whole application, and Spring is used there purely as wiring infrastructure: it discovers architectural stereotypes, registers buses and external clients, and injects concrete implementations into the core’s declared abstractions.
The runtime story then focuses on behavior at the boundaries. A custom transaction bridge lets the architecture’s own transactional annotation work with Spring while supporting handlers that return values instead of throwing exceptions, so rollback can be driven by Either rather than only by exceptions. Domain events are delivered inside the application for local reactions, preferably after commit when secondary effects should not affect the main use case. When communication must cross process boundaries, the chapter introduces integration events and the outbox pattern, ensuring durable publication and consistent delivery to external consumers. In the end, the application is not just well designed in pieces, but operational as a coherent, running hexagon.
Module dependency graph. Inner modules never reference outer ones. The assembly module is the only component that sees everything—its job is to wire, not to contain logic.
Assembly and runtime flow. At startup, Spring discovers beans through stereotype annotations, and the bus assembler creates buses backed by the application context. At runtime, the bus resolves handlers by command type—adapters never reference handlers directly.
The assembled WriteFlow hexagon. The assembly module wires the application at startup, while the runtime structure still respects the hexagonal boundary: input adapters enter through input ports, the bus dispatches to handlers, handlers orchestrate domain behaviour and output ports, and output adapters provide the infrastructure implementations.
Synchronous listeners execute inside the transaction; after-commit listeners execute after. The after-commit listener enters the application as a new use case through the command bus.
From handler to external delivery. The handler calls an output port whose adapter writes the integration event to the outbox within the same transaction. A relay process delivers it to external consumers. Consistency is guaranteed because the outbox write shares the domain state's transaction.
Summary
- Maven modules enforce hexagonal boundaries at compile time. If a dependency is not in the POM, the compiler rejects the import—no runtime discipline required.
- The spring-boot-assembly module is the only component that sees all others. It discovers hexagonal stereotypes (@ApplicationService, @Adapter, @DomainService) through annotation-based component scanning and wires the command and query buses backed by Spring's ApplicationContext.
- The Architecture Kernel's @Transactional annotation declares transactional intent without importing framework concerns. A custom transaction interceptor bridges the gap at runtime—and solves the Either problem: rollback by value, not by exception.
- Transaction boundaries always live at the handler level. The handler is the atomic unit of work—everything it orchestrates succeeds or fails together.
- Domain events decouple secondary effects from the primary use case. After-commit listeners execute only when the primary transaction succeeds, preventing secondary failures from rolling back the primary operation.
- Event listeners are input adapters. They receive a domain event and enter the application core through the command bus—same pattern as a REST controller, different trigger.
- Integration events escape the process boundary. An output port writes them to an outbox table within the same transaction as the domain state change. A relay process delivers them externally. Consumers must be idempotent.
- The assembly module draws a clear line: the application core compiles, runs its logic, and returns results without knowing which framework assembled it.
FAQ
What is the purpose of splitting a hexagonal application into Maven modules?
Maven modules make architectural boundaries enforceable at compile time. They prevent inner layers from importing outer layers by making illegal dependencies a build failure, not just a convention.
What modules make up the WriteFlow architecture in this chapter?
The parent build contains shared-kernel, application-core, rest-adapter, persistence-adapter, notification-adapter, and spring-boot-assembly. Together they represent the architecture as separate build units.
What is inside the application-core module?
The application-core module contains domain, input-ports, output-ports, and application. The domain holds entities and repository interfaces, input-ports holds use case contracts, output-ports holds non-repository output ports, and application contains the handlers.
Why is the assembly module separated from the application core?
The assembly module is the only place that knows about the runtime framework and all other modules. Its job is to wire components together, not to contain business logic, so the core remains framework-independent.
How does Spring connect handlers to their repository implementations at runtime?
Spring discovers the beans through component scanning in the assembly layer, then resolves the interface dependency declared by the handler to the concrete adapter implementation. The handler still depends only on the repository interface, not on the implementation class.
How are the command and query buses wired?
The assembly creates the bus beans explicitly and gives them a registry built from the Spring ApplicationContext. The registry maps message types to handlers, so the bus can dispatch commands and queries without adapters referencing handlers directly.
How does the architecture support transactionality without using Spring’s @Transactional in the core?
The core uses a technology-agnostic @Transactional annotation from the Architecture Kernel. A Spring Boot starter in the assembly layer bridges that annotation to Spring’s transaction infrastructure, so handlers remain framework-independent.
Why does the transaction bridge need special handling for Either?
Because handlers return Either instead of throwing exceptions, Spring would otherwise see a normal method return and commit the transaction. The custom interceptor inspects the Left value and marks the transaction rollback-only when the error matches rollback rules.
Where should transactional boundaries be placed in a hexagonal application?
At the handler level, because the handler orchestrates the whole use case and is the atomic unit of work. Repositories, adapters, and domain services should run inside that boundary, not define their own.
What is the difference between domain events and integration events in this chapter?
Domain events are internal and live within the same application or bounded context, using domain language and types. Integration events are external contracts designed for other systems, with stable, versioned data and durable delivery through the outbox pattern.
Clean Applications with Hexagonal Architecture ebook for free