Skip to main content

Hexagonal Architecture in Spring

2024-03-125 min
Clean CodeJavaSpringArquitectura Hexagonal

Over the past few weeks I've been rediscovering Java, with the goal of sharpening the fundamentals and practicing many common development concepts. Along the way, I also started learning one of its most well-known frameworks, Spring Boot.

To do so I decided to build a small API for a marketplace with 4–5 entities to experiment with how things work.

I also wanted to learn a bit about architecture, so I decided to implement the API following a hexagonal architecture.

My idea was to have the following structure:

javascript
src/main/java/com/sstark/generalmarket/
|-- application/
|   |-- services/
|   |
|   |-- repositories/
|   |
|-- domain/
|   |-- models/
|   |
|-- infrastructure/
|   |-- adapters/
|   |
|   |-- configuration/
|   |
|   |-- entities/
|   |
|   |-- repositories/
|   |
|   |-- controllers/

The most important parts of this structure are the configuration folder and the different adapters, which will be the repositories that Spring injects into the services.

Let's go step by step through the process to make everything work correctly.

Read the full article on Leanmind