Understanding Hexagonal Architecture With a Spring Boot Implementation | by Ramón Bailén | Jun, 2022

An instance utilizing Spring Boot to element the completely different layers of the applying

Photograph by Donny Jiang on Unsplash

Hexagonal Structure was proposed in 2005 by Alistair Cockburn

The primary concept behind this structure is to isolate area logic from exterior elements when designing software program purposes.

Entry to area logic from the surface is on the market via ports and adapters.

A port is simply an interface to be carried out by an adapter. There are two varieties of ports: enter and output. We implement the primary ones within the area layer, whereas the infrastructure module will comprise the implementation of the second ones.

With this strategy, you should have well-defined interfaces to speak out and in of the area layer with out relying on implementation particulars.

Let’s think about a easy Product REST Service to know this structure. Sources of the product service are:

  • Create a Product
  • Get the Product by Id

We begin creating the three layers: utility, infrastructure, and area.


The appliance layer will comprise the ports, that are interfaces that permit inbound or outbound circulation.

We created use circumstances contained in the enter package deal, defining what the person wish to do within the utility. In our instance, create new merchandise and get them by Id.

However, we use the output package deal to connect with some exterior elements.

In our instance, ProductOutputPort will get information from the database, whereas ProductEventPublisher will publish an occasion when a product has been created.


The infrastructure layer represents the outer a part of the hexagonal structure via adapters.

Adapters work together with the core utility solely through the use of the inbound and outbound ports.

We divide the infrastructure.adapters package deal into three new ones:

  1. config: it accommodates the beans of the applying
  2. enter: it drives the applying by invoking the corresponding use case (enter port)
  3. output: it offers an implementation of the output ports like databases or messaging queues


The area layer is the middle of the system. It handles the enterprise logic and represents the applying core.

The area layer is wholly decoupled from utility and infrastructure layers, so modifications within the different layers haven’t any impression on the Product area object until there’s a change within the enterprise necessities.

The ProductService is a vital part contained in the area, because it implements the enter ports and makes use of the output interfaces carried out by the output adapters to return the consequence to the enter adapters:

Area exceptions are thrown on this layer and managed within the infrastructure one (CustomizedExceptionAdapter).

On this article, we noticed via a sensible instance how you can separate the logic associated to our utility into three particular layers: area, utility and infrastructure.

I like to recommend utilizing a mapper library, like MapStruct, to transform objects between the completely different layers.

No two Hexagonal Architectures are the identical, as you could find completely different proposed names for layers and methods to construction your utility. All attainable options ought to make the applying simpler to check, which is the true purpose.

The code for this instance is on the market on GitHub.

More Posts