8.15 Onion architecture — rings per component¶
The backend is cut twice: into components — one package each below net.lauper.feecheck — and,
inside each component, into onion rings. The domain sits innermost and knows nobody, the use
case around it knows the domain, and the adapters on the outside know both; nothing points
inward-out. The rings are declared once, with jMolecules annotations on each ring's
package-info.java, and the build makes that declaration binding: a class that reaches across a
ring boundary fails the build instead of waiting for a reviewer to notice it (from CLAUDE.md,
"Architecture — the decisions already made"; 2.4;
the Javadoc of OnionArchitectureTest).
8.15.1 The three rings¶
| Ring | Annotation | Package | What belongs there |
|---|---|---|---|
| Domain | @DomainRing |
<component>.domain |
the schema, the ports, the prompts, the validation — everything that stays true whichever adapter is configured, or whether a model answered at all |
| Application | @ApplicationRing |
<component>.application |
the application services that drive the domain: orchestration only, no rule of its own |
| Infrastructure | @InfrastructureRing |
<component>.infrastructure |
the adapters and the Spring wiring — everything that knows a technology: a model client, a converter client, a configuration property, a framework annotation, an endpoint |
From CLAUDE.md, "Architecture — the decisions already made" (the table), and the
package-info.java of each ring under backend/app/src/main/java/net/lauper/feecheck/ — for
example extraction's domain ring holds the extraction rules "for exactly that reason: an empty
field is a valid result and an invented one is a bug, and neither statement may depend on the
adapter that happens to be configured". A model provider gets its own sub-package of the
infrastructure ring — extraction.infrastructure.mistral and .gemini — and what all providers
share stays in extraction.infrastructure (CLAUDE.md).
8.15.2 Rings per component, not components inside rings¶
Every component carries its own three rings. That keeps a component self-contained, and it is
the cut each Spring Modulith module is drawn around: each package directly below
net.lauper.feecheck is a module (CLAUDE.md; the Javadoc of ModularityTest). The modules
are cases, consumer, extraction, intake, kernel, mcp, signin and webapp —
ModularityTest.modulesVerify asserts exactly that list; their responsibilities and dependencies
are the white box of 5.2.1.
FeeCheckApplication sits in the root package, so component scanning reaches every component
without being edited (CLAUDE.md; 5.2.1).
8.15.3 Dependency direction¶
The port is declared in the domain ring and implemented by an adapter in the infrastructure ring;
the application service calls the port and never knows which adapter answers. That is what keeps the
stub/Mistral swap a change of configuration rather than of domain code
(8.13; the package-info.java of
extraction.infrastructure). Between components the dependency runs in pipeline direction:
extraction depends on intake's domain, and intake knows nothing of extraction (CLAUDE.md).
flowchart LR
subgraph extraction["extraction"]
infra["infrastructure<br/>adapters · Spring"]
app["application<br/>service"]
dom["domain<br/>port · schema"]
end
subgraph intake["intake"]
idom["domain<br/>published"]
end
infra --> app
infra --> dom
app --> dom
dom --> idom
classDef person fill:#ffe8b0,stroke:#9a6b00,color:#000
classDef built fill:#b7e4c7,stroke:#2d6a4f,color:#000
classDef planned fill:#c6dcff,stroke:#1168bd,stroke-dasharray:4 3,color:#000
classDef external fill:#e6e6e6,stroke:#6b6b6b,color:#000
class infra,app,dom,idom built
style extraction fill:none,stroke:#666
style intake fill:none,stroke:#666
| Colour | Meaning |
|---|---|
| green | built |
An arrow points from the ring that uses to the ring it uses.
| Element | Holds | Depends on | Code (directory) | Status |
|---|---|---|---|---|
| extraction · domain | the port DocumentExtractor, the claim schema ExtractionClaim, ExtractionResult with its FieldConfidence, ExtractionPrompt, ExtractionResponseParser |
intake's published domain types — the port takes intake's Rendition |
extraction/domain |
built |
| extraction · application | the application service DocumentExtractionService |
its own domain | extraction/application |
built |
| extraction · infrastructure | the adapters behind the port — stub, Mistral, Gemini — the loop ExtractionLoop and the Spring wiring that selects one |
both inner rings; Spring AI | extraction/infrastructure |
built |
| intake · domain | Rendition, ConversionResult, the port DocumentConverter, published as the named interface domain |
nothing of extraction | intake/domain |
built |
Directories below backend/app/src/main/java/net/lauper/feecheck/; the contents from the
5.2.1 tree and the package-info.java
files.
Why rings and not jMolecules' layers. In the layered model of the DDD book the infrastructure
layer is the bottom of the stack that everything else may use, so jMolecules'
ensureLayering() forbids precisely the dependency this design is built on — an adapter
implementing a port the domain declares. The onion model states the direction the project actually
has (CLAUDE.md; the Javadoc of OnionArchitectureTest).
8.15.4 Ports and adapters in the rings¶
A port is an interface the domain ring declares for something it needs from outside; an outbound
adapter in the infrastructure ring implements it, and configuration decides which adapter is the
bean. An inbound adapter — an HTTP controller, the queue's push endpoint — receives a request and
calls the application ring. The worked example is intake's converter, as the case's upload uses it
(the classes below backend/app/src/main/java/net/lauper/feecheck/).
flowchart LR
subgraph cases["cases"]
caseCtl["CaseController<br/>infrastructure"]
caseSvc["CaseService<br/>application"]
end
subgraph intake["intake"]
convCtl["ConversionController<br/>infrastructure"]
intakeSvc["DocumentIntakeService<br/>application"]
port["DocumentConverter<br/>domain · port"]
stub["StubDocumentConverter<br/>infrastructure"]
docl["DoclingDocumentConverter<br/>infrastructure"]
end
docling["docling-serve"]
caseCtl --> caseSvc
caseSvc --> intakeSvc
intakeSvc --> port
convCtl --> port
stub -->|"implements"| port
docl -->|"implements"| port
docl --> docling
classDef person fill:#ffe8b0,stroke:#9a6b00,color:#000
classDef built fill:#b7e4c7,stroke:#2d6a4f,color:#000
classDef planned fill:#c6dcff,stroke:#1168bd,stroke-dasharray:4 3,color:#000
classDef external fill:#e6e6e6,stroke:#6b6b6b,color:#000
class caseCtl,caseSvc,convCtl,intakeSvc,port,stub,docl built
class docling external
style cases fill:none,stroke:#666
style intake fill:none,stroke:#666
| Colour | Meaning |
|---|---|
| green | built |
| grey | external system |
An arrow points from the class that uses to the class it uses; "implements" points from an adapter to its port.
| Element | Role | Ring · package | Status |
|---|---|---|---|
CaseController |
inbound adapter: POST /api/cases takes the upload; its constructor builds CaseService with a DocumentIntakeService on the converter bean |
infrastructure · cases.infrastructure |
built |
CaseService |
the case's application service; converts through intake's published DocumentIntakeService |
application · cases.application |
built |
ConversionController |
inbound adapter: GET /api/intake/converter reports the converter's readiness, so it asks the port directly — no application service in between |
infrastructure · intake.infrastructure |
built |
DocumentIntakeService |
intake's application service: takes an upload to the port; published | application · intake.application |
built |
DocumentConverter |
the port: convert and readiness; published |
domain · intake.domain |
built |
StubDocumentConverter |
outbound adapter with prepared pages, no network — the default | infrastructure · intake.infrastructure |
built |
DoclingDocumentConverter |
outbound adapter calling docling-serve through docling-java (ADR-0004 (docs/adr/adr-0004-document-conversion-docling.md)) |
infrastructure · intake.infrastructure |
built |
| docling-serve | the document converter service (7.1) | — | external |
How it maps to Spring and jMolecules. jMolecules states the architecture in the code and
carries no runtime behaviour; Spring does the wiring in the infrastructure ring, and the inner rings carry no Spring code but Spring Modulith's @NamedInterface on a published ring's package-info.java
(backend/app/build.gradle.kts, the comment on the jMolecules dependencies).
| Element | jMolecules | Spring | Where (package) |
|---|---|---|---|
| Ring | @DomainRing, @ApplicationRing, @InfrastructureRing (org.jmolecules.architecture.onion.simplified) on the ring's package-info.java |
— | <module>.domain, .application, .infrastructure |
| Value | @ValueObject (org.jmolecules.ddd.annotation) on a record, e.g. Rendition |
— | domain |
| Port | — a plain Java interface, e.g. DocumentConverter, CaseStore |
— | domain |
| Application service | @Service (org.jmolecules.ddd.annotation, not Spring's stereotype), e.g. DocumentIntakeService, CaseService |
no stereotype: an application service becomes a bean through an @Bean method (DocumentExtractionService in ExtractionConfiguration) or is built by the inbound adapter on beans (DocumentIntakeService and CaseService in CaseController's constructor) |
application |
| Outbound adapter | — | a class implementing the port, made a bean by an @Bean method of the module's @Configuration and chosen by @ConditionalOnProperty — intake.converter = stub (the default, matchIfMissing) or docling in IntakeConfiguration; never by an if in domain code (8.13) |
infrastructure |
| Persistence | — the domain carries no mapping annotation yet; jMolecules' @Identity marks the ID where a domain record is mapped, and its type-based model is not used for the aggregates, Spring Data's mapping annotations in a domain ring only the fallback (ADR-0019 (docs/adr/adr-0019-spring-data-jdbc-for-the-relational-store.md)) |
Spring Data JDBC: a persistence-shaped record with Spring Data's @Id and a repository of @Query methods, wired by Spring Boot only in a database profile |
infrastructure |
| Inbound adapter | — | @RestController, e.g. CaseController, ConversionController |
infrastructure |
| Wiring | — | constructor injection only, never @Autowired on a field (CLAUDE.md, "Coding conventions") |
infrastructure |
| Published ring | — | Spring Modulith's @NamedInterface on the ring's package-info.java (8.16) |
the published ring |
From IntakeConfiguration.java (the two @Bean methods with @ConditionalOnProperty),
CaseController.java (its constructor), ExtractionConfiguration.java
(documentExtractionService), ConversionController.java, DocumentConverter.java,
Rendition.java, DocumentIntakeService.java and the package-info.java files.
8.15.5 How the build enforces it¶
Both test classes run in test, so in ./gradlew build
(8.4).
| Check | What it enforces | Where |
|---|---|---|
ringsAreRespected |
jMolecules' ensureOnionSimple(): no class reaches across a ring boundary inward-out, so the domain knows nothing of the adapters that implement its port |
OnionArchitectureTest |
dddBuildingBlocksAreUsedCorrectly |
jMolecules' DDD rules (JMoleculesDddRules.all()), e.g. a value object does not refer to identifiable state |
OnionArchitectureTest |
webLayerStaysInTheAdapters |
no class in a domain or application ring depends on org.springframework.web or jakarta.servlet — the web layer is an adapter |
OnionArchitectureTest |
consumerReachesTheCaseOnlyThroughItsPorts |
the consumer module uses nothing of cases outside cases.domain, so it can become a worker deployable by a change of deployment, not of code (JB-14 (docs/specs/263-intake-job.md), ADR-0009 §5) |
OnionArchitectureTest |
kernelDependsOnNothingInThisProject |
the shared kernel depends on nothing else in net.lauper.feecheck (ADR-0007) |
OnionArchitectureTest |
modulesMeetOnlyThroughPublishedTypes |
modules — defined by the packages net.lauper.feecheck.(*).. — depend on each other only through the kernel or a type on an explicit list, published on purpose with the module that offers it |
OnionArchitectureTest |
modulesVerify |
Spring Modulith's ApplicationModules.verify(): no module uses another module's internals and no cycle runs between modules; the module list is the eight above (ARC-Q5.3) |
ModularityTest |
cycleFails, internalUseFails |
verify() on planted violations under net.lauper.modulithprobe in the test tree, so a verify() that could not fail fails here instead of passing silently |
ModularityTest |
moduleDocsAreWritten |
the generated module view — components.puml and one canvas per module — in backend/app/build/spring-modulith-docs |
ModularityTest |
From the code of backend/app/src/test/java/net/lauper/feecheck/OnionArchitectureTest.java and
ModularityTest.java. OnionArchitectureTest imports the main classes only
(ImportOption.DoNotIncludeTests): a test class carries no ring annotation, while
ArchitectureTest has to see the tests to assert their @DisplayName — the reason they are two
classes (its Javadoc).
Which ring of a module is published, and how modules use each other, is in 8.16.
8.15.6 The shared kernel¶
net.lauper.feecheck.kernel holds small value types that more than one module uses, and nothing
else (ADR-0007 (docs/adr/adr-0007-shared-kernel-of-value-types.md)). It is itself a domain ring
(@DomainRing), so the domain of every module may use it, and it depends on nothing in this
project — kernelDependsOnNothingInThisProject enforces that, and modulesMeetOnlyThroughPublishedTypes
lets every module reach it. A type enters only by ADR-0007's rule: used by at least two modules, a
value compared by its content, no behaviour that belongs to one module, no dependency on anything
in this project. A type that owns one module's format stays in that module and is published by it,
as intake publishes Rendition. The kernel is empty so far; its first candidate, extraction's
PageReference, moves there when verification uses it (the kernel's package-info.java; ADR-0007
§3).
8.15.7 Modules without all three rings¶
A module carries only the rings it has something for (each module's package-info.java):
| Module | Rings | Why |
|---|---|---|
mcp |
infrastructure | the MCP server is an adapter and nothing else; it reads extraction's published domain types, in pipeline direction, and no other component knows it |
signin |
infrastructure | sign-in with Google (ADR-0008) is an adapter and nothing else; the provider is configuration |
webapp |
infrastructure | serving the built frontend/app as static resources is an adapter and nothing else |
consumer |
application, infrastructure | the queue's consumer is a module of its own beside the case component — a module holds no other module — and reaches the case only through its ports in cases.domain; intake and extraction it reaches through their published types (ADR-0009 §5; 5.2.1) |