Quick revision
The short answer
Hexagonal architecture, also called ports and adapters, puts the domain logic and use cases in an application core that depends on nothing technical, and connects it to the outside world through ports, which are interfaces at the core's boundary. Driving or primary adapters, such as REST controllers, message listeners and schedulers, call into the core; driven or secondary adapters, such as database repositories, message publishers and external API clients, are called by the core through ports. Because all dependencies point inward, the core can be tested with simple in-memory adapters.
What to remember
In this answer, we saw that hexagonal architecture keeps domain logic and use cases in a core that depends on nothing technical, with ports as the interfaces at its boundary. Driving adapters, such as REST controllers, listeners and schedulers, call into the core through inbound ports, while driven adapters, such as repositories, publishers and API clients, are called by the core through outbound ports that the core owns. Because all source dependencies point inward, the core can be tested quickly with in-memory adapters.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Hexagonal architecture, also called ports and adapters, puts the domain logic and use cases in an application core that depends on nothing technical, and connects it to the outside world through ports, which are interfaces at the core's boundary. Driving or primary adapters, such as REST controllers, message listeners and schedulers, call into the core; driven or secondary adapters, such as database repositories, message publishers and external API clients, are called by the core through ports. Because all dependencies point inward, the core can be tested with simple in-memory adapters.
Why this question is asked
Hexagonal architecture comes up in interviews because it is the name many teams give to the idea of keeping business logic independent of frameworks, and because the terms port, adapter, driving and driven are easy to mix up. If we understand which way each dependency points, the rest follows naturally. In this answer, we will recap the problem the style solves, define the core, ports and adapters, separate driving from driven adapters with an example, show why dependencies point inward, and see how that lets us test the core without Spring or a database.
1The Problem Hexagonal Architecture Solves
In a plain layered application, the service layer sits on top of the persistence layer, so the business logic depends on JPA repositories and often on HTTP types and messaging libraries too. Testing a rule then needs a database or a lot of mocking, and replacing the database or adding a new way to trigger the use case means touching business code.
Alistair Cockburn proposed hexagonal architecture in the mid-2000s to fix this. The idea is that the application should be equally happy being driven by a user interface, a test, a batch job or another program, and equally happy storing data in a real database or in memory. The hexagon shape has no meaning of its own; it just leaves room to draw several adapters around the edges.
2The Core, Ports and Adapters
The application core holds the domain model, such as Order and Money, and the use cases, such as placing an order. It is plain Java and imports nothing technical: no Spring web types, no JPA annotations, no Kafka classes.
A port is an interface at the boundary of the core. It is written in the core's language and owned by the core. Some ports describe what the core offers, for example a PlaceOrderUseCase interface, and some describe what the core needs from the outside, for example an OrderRepository or a PaymentGateway interface.
An adapter is the technical code that connects a port to a real technology. A REST controller is an adapter, and so is a JPA implementation of OrderRepository. Adapters know about frameworks; the core does not.
- Driving adaptersREST controller, Kafka listener, scheduler, tests
- Inbound ports
PlaceOrderUseCaseinterface - Application coreOrder, Money,
PlaceOrderService; plain Java - Outbound ports
OrderRepository,PaymentGatewayinterfaces - Driven adaptersJPA repository, Kafka publisher, payment HTTP client
5 layers, from the top down. Step through them one at a time.
Both kinds of adapter depend on ports defined by the core; the core depends on nothing outside it.
3Driving Versus Driven Adapters
The difference is who starts the conversation. A driving adapter, also called a primary adapter, takes an event from the outside world and drives the application by calling into the core. A REST controller receiving POST /orders, a Kafka listener receiving a message, and a scheduler firing at midnight are all driving adapters. They call an inbound port.
A driven adapter, also called a secondary adapter, is called by the core when the core needs something done. The core says save this order or charge this card through an outbound port, and the driven adapter does it with JPA, a message broker or an HTTP client. So database repositories, message publishers and clients for external APIs are driven adapters.
Notice that PlaceOrderService has no Spring annotation and no JPA import. The controller depends on the inbound port, and the JPA adapter implements the outbound port. Both point at the core.
4Why Dependencies Point Inward
The key move is dependency inversion on the driven side. In a layered application, the service would call a JPA repository directly, so the source code dependency would point from business logic to persistence. Here the core defines the OrderRepository interface, and the JPA class implements it. At runtime the call still goes from the core to the database, but the source code dependency points from the adapter to the core.
So the core can be compiled, understood and tested on its own. Replacing JPA with JDBC, or adding a gRPC endpoint next to REST, means writing a new adapter. The core does not change, because it never knew which adapter was on the other side of the port.
5Testing the Core With In-Memory Adapters
Because the core only knows interfaces, we can plug in tiny in-memory implementations in tests. No Spring context, no database, no HTTP. These tests run in milliseconds.
The adapters still need their own tests, such as a Testcontainers test for the JPA adapter and a web slice test for the controller, but the business rules are covered by fast, focused tests.
6Follow-up questions
Is a port always an interface?
In Java it almost always is, because an interface is how the core states what it needs without depending on an implementation. An inbound port is sometimes skipped and the controller calls the use case class directly, which is a reasonable simplification; the outbound ports are the ones that give the dependency inversion.
How does Spring fit in if the core has no annotations?
Spring lives in the adapters and in a configuration class that creates the core objects as beans, for example a @Bean method that returns new PlaceOrderService(orders, payments). Spring then injects the JPA adapter and the payment client into it. The core stays plain Java while Spring does the wiring.
Where does @Transactional go?
Common options are a thin transactional application service in an adapter or configuration layer that wraps the use case, or allowing @Transactional on use case classes as a pragmatic exception. Teams choose one and enforce it consistently.
7Conclusion
In this answer, we saw that hexagonal architecture keeps domain logic and use cases in a core that depends on nothing technical, with ports as the interfaces at its boundary. Driving adapters, such as REST controllers, listeners and schedulers, call into the core through inbound ports, while driven adapters, such as repositories, publishers and API clients, are called by the core through outbound ports that the core owns. Because all source dependencies point inward, the core can be tested quickly with in-memory adapters.
Open with
Hexagonal architecture, or ports and adapters, puts the business logic and use cases in a core that depends on nothing technical: no Spring web types, no JPA, no Kafka.
Then, if they want more
The core talks to the outside world through ports, which are interfaces it defines at its boundary, and adapters connect those ports to real technology. There are two kinds of adapter, and the difference is who starts the call.
Driving, or primary, adapters call into the core: a REST controller, a message listener or a scheduler receives something from outside and calls an inbound port such as PlaceOrderUseCase.
Driven, or secondary, adapters are called by the core: when the use case needs to save an order or charge a card, it calls an outbound port like OrderRepository or PaymentGateway, and a JPA repository, a message publisher or an HTTP client implements it. The important part is that dependencies point inward.
The core owns the outbound interfaces, and the adapters implement them, so at runtime the core calls the database, but in the source code the database adapter depends on the core. That means I can test the use cases with in-memory adapters in milliseconds, without Spring or a database, and swap or add adapters without touching business code.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain hexagonal architecture (ports and adapters). What is the difference between...": https://scrivio-interview-library.netlify.app/java/application-architecture/9a3f690817/