Scrivio Interview library
DesignIntermediate7 min read · updated 12 November 2026

Structure a Spring Boot payment service in hexagonal style. Describe the modules or packages, where Spring annotations are allowed, and how you enforce the structure.

We split the payment service into a domain module with entities, value objects and domain services and no framework dependencies, an application module with use cases and port interfaces that allows at most transaction annotations, adapter modules for web, JPA and payment provider clients that use Spring freely, and a bootstrap module that wires adapters to ports. Maven or Gradle modules make illegal imports fail compilation, or ArchUnit tests enforce the same rules, and we accept the cost of mapping between web DTOs, domain objects and persistence entities.

Why this question is asked

A payment service is a good fit for hexagonal architecture. It has real rules about amounts, currencies, states and idempotency, it talks to external payment providers that change or get replaced, and it must be tested thoroughly without depending on a provider's sandbox being up. Interviewers ask this design question to see whether we can turn the idea of ports and adapters into a concrete structure that a team can work in, with clear rules about where Spring is allowed. In this answer, we will lay out the modules, walk through what goes in each one, decide where annotations are allowed, wire everything together, enforce the structure, and look honestly at the mapping cost.

1The Module Layout

We use Maven modules rather than only packages, because separate modules give us compile-time enforcement for free: if the domain module does not declare a dependency on Spring, no class in it can import Spring. The service is still one deployable application; modules are a build structure, not separate services.

Text
payment-service/
payment-domain/ Payment, Money, PaymentState, RefundPolicy (no dependencies)
payment-application/ AuthorizePayment, RefundPayment use cases
ports: PaymentRepository, PaymentProvider, EventPublisher
(depends on payment-domain, spring-tx for @Transactional only)
payment-adapter-web/ PaymentController, request and response DTOs (spring-web)
payment-adapter-jpa/ PaymentEntity, JpaPaymentRepository (spring-data-jpa)
payment-adapter-stripe/ StripePaymentProvider, provider DTOs (http client)
payment-adapter-kafka/ KafkaEventPublisher (spring-kafka)
payment-bootstrap/ PaymentApplication, wiring @Configuration (depends on all)
Step throughPayment service modules and their dependencies
  1. payment-bootstrap@SpringBootApplication, wires adapters to ports
  2. Adapter modulesweb, jpa, stripe, kafka; Spring used freely
  3. payment-applicationuse cases and ports; @Transactional only
  4. payment-domainPayment, Money, states, rules; plain Java

4 layers, from the top down. Step through them one at a time.

Each module may depend only on the modules below it.

The direction is the whole point. Adapters depend on the application module, the application depends on the domain, and the domain depends on nothing. Only the bootstrap module sees everything.

2The Domain Module: Rules Without Frameworks

The domain module holds the payment model. Payment is an entity in the domain-driven design sense, an object with an identity and a life cycle. Money is a value object, an immutable object defined only by its values, here an amount and a currency. Domain services hold rules that do not belong to one object, such as a refund policy that depends on the payment and the merchant's settings.

Notice what the domain enforces: a payment cannot be captured twice, a refund cannot exceed the captured amount, and money never mixes currencies. None of these rules need a database or HTTP to test.

Java
public record Money(BigDecimal amount, Currency currency) {
public Money { if (amount.signum() < 0) throw new IllegalArgumentException("negative"); }
public Money minus(Money other) {
if (!currency.equals(other.currency)) throw new IllegalArgumentException("currency mismatch");
return new Money(amount.subtract(other.amount), currency);
}
}
public class Payment {
private final PaymentId id;
private PaymentState state = PaymentState.AUTHORIZED;
private Money captured;
// ...
public void refund(Money amount) {
if (state != PaymentState.CAPTURED) throw new IllegalStateException("not captured");
captured = captured.minus(amount); // throws if amount is larger: negative
}
}

3The Application Module: Use Cases and Ports

The application module holds one class per use case, such as AuthorizePayment and RefundPayment, and the port interfaces the use cases need: PaymentRepository to load and save payments, PaymentProvider to talk to the card processor, and EventPublisher to announce that a payment was captured. Each use case loads the domain object through a port, calls domain methods, saves the result and publishes events.

This is where we allow one framework annotation: @Transactional, because a use case is the natural transaction boundary. Teams that want a completely framework-free application module can instead wrap use cases in a transactional decorator in the bootstrap module; both are defensible, and the rule must simply be written down. Nothing else from Spring is allowed here: no @Service, no @Autowired, no web or JPA types.

Java
public class RefundPayment {
private final PaymentRepository payments;
private final PaymentProvider provider;
private final EventPublisher events;
public RefundPayment(PaymentRepository p, PaymentProvider pr, EventPublisher e) { payments = p; provider = pr; events = e; }
@Transactional
public RefundResult refund(PaymentId id, Money amount, IdempotencyKey key) {
Payment payment = payments.get(id);
payment.refund(amount); // domain rule
ProviderRefund r = provider.refund(payment.providerRef(), amount, key);
payments.save(payment);
events.publish(new PaymentRefunded(id, amount));
return RefundResult.of(r);
}
}

In a real payment service we would be careful about calling the provider inside a database transaction and about publishing events reliably, for example with an outbox table. Those concerns live in the adapters and the wiring, and the use case code barely changes.

4Adapters and the Bootstrap Module

Adapter modules use Spring freely. The web adapter has @RestController classes and request DTOs with validation annotations. The JPA adapter has PaymentEntity with @Entity and a Spring Data repository, and a JpaPaymentRepository class that implements the PaymentRepository port by mapping between the entity and the domain Payment. The Stripe adapter implements PaymentProvider with an HTTP client and the provider's own DTOs, so the provider's vocabulary never reaches the core.

The bootstrap module contains the @SpringBootApplication class and a configuration class that creates use cases as beans. Because use case classes are plain Java, Spring sees them only through these @Bean methods.

Java
@Configuration
class UseCaseConfig {
@Bean RefundPayment refundPayment(PaymentRepository payments, PaymentProvider provider, EventPublisher events) {
return new RefundPayment(payments, provider, events); // Spring injects the adapters
}
}

5Enforcing the Structure

Maven modules give us the strongest enforcement: payment-domain declares no dependencies, so an import of org.springframework in it does not compile. The application module declares only payment-domain and spring-tx, so it cannot use web or JPA types. We can add the Maven Enforcer plugin to forbid banned dependencies being added later.

Where a team prefers one module with packages, ArchUnit tests can express the same rules: classes in ..domain.. may depend only on java.., classes in ..application.. may not depend on ..adapter.., and adapters may not depend on each other. Many teams use both: modules for the big boundaries, ArchUnit for finer rules such as no adapter talking to another adapter.

6The Cost: Mapping Between Models

This structure has a real cost. A refund request arrives as a RefundRequest DTO, becomes a command and domain objects in the core, and is stored as a PaymentEntity, so each boundary needs mapping code and tests for it. For a payment service, we accept this, because each model changes for different reasons: the API must stay stable for merchants, the domain rules change with payment regulation, and the persistence model changes with performance needs. MapStruct can generate the mechanical mappings, leaving hand-written code only where translation involves decisions.

If the service were a simple lookup of merchant settings, the same cost would buy almost nothing, and a plain layered design would be the better choice.

7Follow-up questions

Why not put JPA annotations directly on the domain Payment class?

It saves a mapping, and some teams do it pragmatically. The cost is that JPA requirements, such as a no-argument constructor, mutable fields and lazy proxies, leak into the domain, and the domain module now depends on Jakarta Persistence. For a payment domain with strict invariants, a separate entity keeps the model clean.

How do you test this structure?

Domain and use case tests are plain JUnit tests with in-memory fakes for the ports, and they cover the business rules. Each adapter gets focused integration tests, such as Testcontainers for the JPA adapter and WireMock for the Stripe client. A small number of end-to-end tests in the bootstrap module check that the wiring works.

8Conclusion

In this answer, we structured a payment service in hexagonal style. The domain module holds Payment, Money and the payment rules with no framework dependencies; the application module holds use cases and ports and allows only @Transactional; adapter modules for web, JPA, the payment provider and Kafka use Spring freely; and a bootstrap module wires adapters to ports with @Bean methods. Maven modules make illegal imports fail compilation and ArchUnit covers finer rules, and we accept the mapping cost because each model changes for its own reasons.

What to say in the interviewAbout 90 seconds aloud

Open with

I would use separate Maven or Gradle modules inside one deployable service. The domain module holds Payment, Money as a value object, payment states and domain services like the refund policy, with no dependencies at all, so rules such as no refund larger than the captured amount are plain Java and fast to test.

Then, if they want more

The application module holds one class per use case, like AuthorizePayment and RefundPayment, plus the port interfaces they need: PaymentRepository, PaymentProvider and EventPublisher. There I would allow only @Transactional, since the use case is the transaction boundary, and nothing else from Spring.

Then adapter modules where Spring is used freely: a web adapter with controllers and request DTOs, a JPA adapter with entities and a repository that implements the port by mapping, a provider adapter that wraps the card processor's API so its vocabulary never reaches the core, and a Kafka publisher.

A bootstrap module has the Spring Boot application and @Bean methods that construct the use cases with the adapters. Enforcement comes mainly from the build: the domain module simply cannot import Spring because it does not declare it, and ArchUnit covers finer rules like adapters not calling each other.

The cost is mapping between DTOs, domain objects and entities, which I accept for payments, and reduce with MapStruct for the mechanical parts.

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 "Structure a Spring Boot payment service in hexagonal style. Describe the modules or...": https://scrivio-interview-library.netlify.app/java/application-architecture/7174d0d669/