Blog
Hexagonal Architecture In Java: Ports Must Earn Their Names
Hexagonal architecture is often introduced as a folder tree with domain, application, ports, and adapters. Teams copy it, create forty one-method interfaces, and declare victory.
A boundary earns its place when it protects policy from a volatile detail.
Start With One Use Case
final class ReserveFunds {
private final Accounts accounts;
private final Reservations reservations;
ReservationId execute(ReserveCommand command) {
Account account = accounts.load(command.accountId());
Reservation reservation = account.reserve(command.amount());
return reservations.save(reservation);
}
}
Accounts and Reservations are ports if the use case should not know JDBC, JPA, HTTP, or an in-memory test implementation. The domain operation remains ordinary Java.
Spring wiring belongs outside this core. An adapter can implement the port with JPA. A controller can convert JSON into a command.
Do Not Abstract Stable Language Features
An interface around every utility, mapper, and value object adds navigation without isolation. “We may replace it later” is not enough. Name the concrete volatility: database technology, external provider, clock, message publication, filesystem.
Test At Different Seams
Pure policy tests run without Spring. Adapter integration tests prove SQL and mapping. A smaller set of end-to-end tests proves wiring.
This is the real payoff: tests select the boundary appropriate to the risk. Mocking every internal class is not hexagonal architecture. It is fear of objects touching.
If the architecture diagram has many ports but a business rule still lives in a controller annotation callback, the folders are lying. Trace a use case from input to state change; the policy should remain readable without framework archaeology.