hexagonal-architecture
/hexagonal-architecture
Use when the project follows hexagonal (ports & adapters) architecture. Prevents domain code from depending on Spring or JPA. Use when you see packages like domain/, application/, infrastructure/, or adapters/ in the project structure.
- Author
- rrezartprebreza ↗
- Repository
- spring-boot-skills ↗
- Licence
- MIT
- Last repository change
- 22.10.2026
Assessment and security
- Files in this package
- 6
- Scripts
- 4
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 22.10.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
- 4 scripts in this package. Review them before installing, as Claude may execute them.
Scripts: examples/bad-use-case.java, examples/good-use-case.java, templates/OrderJpaAdapter.java, templates/OrderJpaEntity.java
What it does and when to use it
Use when the project follows hexagonal (ports & adapters) architecture. Prevents domain code from depending on Spring or JPA. Use when you see packages like domain/, application/, infrastructure/, or adapters/ in the project structure.
Original author description
Use when the project follows hexagonal (ports & adapters) architecture. Prevents domain code from depending on Spring or JPA. Use when you see packages like domain/, application/, infrastructure/, or adapters/ in the project structure.
How it works
- Package Structure
- Domain Layer — Zero Spring
- Ports — Interfaces Only
- Application Layer — Use Case Implementation
- Infrastructure — Adapters
- Gotchas
SKILL.md preview
---
name: hexagonal-architecture
description: >
Use when the project follows hexagonal (ports & adapters) architecture. Prevents domain
code from depending on Spring or JPA. Use when you see packages like domain/, application/,
infrastructure/, or adapters/ in the project structure.
---
# Hexagonal Architecture
## Package Structure
```
src/main/java/com/example/
├── domain/ ← Pure Java. Zero framework dependencies.
│ ├── model/ ← Entities, value objects, aggregates
│ ├── port/
│ │ ├── in/ ← Use case interfaces (driving ports)
│ │ └── out/ ← Repository/external interfaces (driven ports)
│ └── service/ ← Domain services (pure business logic)
├── application/ ← Orchestrates use cases. Spring allowed here.
│ └── usecase/ ← @Service implementations of domain ports
└── infrastructure/ ← All framework/DB/HTTP details
├── persistence/ ← JPA adapters implementing out ports
├── web/ ← REST controllers (driving adapters)
└── external/ ← HTTP clients, messaging adapters
```
## Domain Layer — Zero Spring
```java
// domain/model/Order.java — pure Java, no annotations
public class Order {
private final OrderId id;
private final CustomerId customerId;
private OrderStatus status;
private final List<OrderItem> items;
private Order(OrderId id, CustomerId customerId) {
this.id = id;
this.customerId = customerId;
this.status = OrderStatus.PENDING;
this.items = new ArrayList<>();
}
public static Order create(CustomerId customerId) {
return new Order(OrderId.generate(), customerId);
}
public void addItem(ProductId productId, int quantity, Money price) {
if (status != OrderStatus.PENDING)
throw new OrderNotModifiableException(id);
items.add(new OrderItem(productId, quantity, price));
}
// Getters only — no setters
}
// domain/model/OrderId.java — value object
public record OrderId(UUID value) {
public static OrderId generate() { return new OrderId(UUID.randomUUID()); }
public static OrderId of(String value) { return new OrderId(UUID.fromString(value)); }
}
```
## Ports — Interfaces Only
```java
// domain/port/in/CreateOrderUseCase.java — driving port
public interface CreateOrderUseCase {
Order createOrder(CreateOrderCommand command);
}
// domain/port/in/CreateOrderCommand.java
public record CreateOrderCommand(CustomerId customerId, List<OrderItemData> items) {}
// domain/port/out/OrderRepository.java — driven port
public interface OrderRepository {
Order save(Order order);
Optional<Order> findById(OrderId id);
void deleteById(OrderId id);
}
// domain/port/out/InventoryPort.java — driven port
public interface InventoryPort {
void reserve(List<OrderItem> items);
void release(List<OrderItem> items);
}
```
## Application Layer — Use Case Implementation
```java
// application/usecase/CreateOrderService.java
@Service // Spring allowed here
@RequiredArgsConstructor
@Transactional
public class CreateOrderService implements CreateOrderUseCase {
private final OrderRepository orderRepository; // domain port (not JPA repo)
private final InventoryPort inventoryPort; // domain port
@Override
public Order createOrder(CreateOrderCommand command) {
Order order = Order.create(command.customerId());
command.items().forEach(item ->
order.addItem(item.productId(), item.quantity(), item.price()));
inventoryPort.reserve(order.getItems());
return orderRepository.save(order);
}
}
```
## Infrastructure — Adapters
```java
// infrastructure/persistence/JpaOrderRepository.java — implements domain port
@Repository
@RequiredArgsConstructor
public class JpaOrderRepository implements OrderRepository {
private final SpringDataOrderRepository springDataRepo;
private final OrderMapper mapper;
@Override
public Order save(Order order) {
OrderJpaEntity entity = mapper.toEntity(order);
return mapper.toDomain(springDataRepo.save(entity));
}
@Override
public Optional<Order> findById(OrderId id) {
return springDataRepo.findById(id.value()).map(mapper::toDomain);
}
@Override
public void deleteById(OrderId id) {
springDataRepo.deleteById(id.value());
}
}
// Separate Spring Data interface — infrastructure detail
interface SpringDataOrderRepository extends JpaRepository<OrderJpaEntity, UUID> {}
// infrastructure/web/OrderController.java — driving adapter
@RestController
@RequestMapping("/api/v1/orders")
@RequiredArgsConstructor
public class OrderController {
private final CreateOrderUseCase createOrderUseCase; // injects use case port
@PostMapping
public ResponseEntity<ApiResponse<OrderResponse>> create(@Valid @RequestBody CreateOrderRequest request) {
Order order = createOrderUseCase.createOrder(request.toCommand());
return ResponseEntity.status(201).body(ApiResponse.ok(OrderResponse.from(order)));
}
}
```
## Gotchas
- Agent imports `jakarta.persistence` in domain classes — domain must be framework-free
- Agent injects `JpaRepository` directly into use cases — use domain port interfaces
- Agent puts `@Transactional` on domain services — belongs in application layer
- Agent mixes driving and driven ports — `port/in` = what app offers, `port/out` = what app needs
- Agent creates anemic domain with only getters/setters — behavior belongs on domain objects
- Agent stubs driven ports with `@MockBean` in tests — removed in Boot 4; use `@MockitoBean`
- Agent puts the adapter's `spring-boot-starter-aop` dependency for port proxies — renamed `spring-boot-starter-aspectj` in Boot 4
Files in this package (6)
- SKILL.md 6 KB
- agents/openai.yaml 1 KB
- examples/bad-use-case.java 2 KB
- examples/good-use-case.java 2 KB
- templates/OrderJpaAdapter.java 1 KB
- templates/OrderJpaEntity.java 2 KB
Versions
| Repository | Score | Licence |
|---|---|---|
| This page · rrezartprebreza/spring-boot-skills | 100 | MIT |
| affaan-m/ECC | 100 | MIT |
| rrezartprebreza/spring-boot-skills | 100 | MIT |
| SHRIJANAGAIN/Nemo-Cli | 100 | Apache-2.0 |
| zernie/vigiles | 100 | MIT |
| bdfinst/agentic-dev-team | 100 | MIT |
| fatonh/symfony-skills | 100 | MIT |
| FlameMida/spec-dev | 95 | MIT |
| packmindhub/packmind | 95 | Apache-2.0 |
Database snapshot: 13.11.2026 · package version b36a633cfefb (SHA-256 digest of the entire package)
Downloads in the last 30 days: 1 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 100ddd-architecture-hexagonal
Comprehensive guidance for Hexagonal Architecture (Ports & Adapters) — Alistair Cockburn's hexagonal architecture with domain core, port definitions, adapter implementations, and full Java/Spring Boot implementation steps. Covers primary/driving adapters, secondary/driven… - 100707-technologies-hexagonal-architecture
Use when you need framework-agnostic Hexagonal architecture guidance for Java projects - ports and adapters boundaries, application core independence, driving and driven adapters, dependency direction, infrastructure leakage detection, optional architecture tests, and… - 100clean-architecture
Provides implementation patterns for Clean Architecture, Domain-Driven Design (DDD), and Hexagonal Architecture (Ports & Adapters) in NestJS/TypeScript applications. Use when structuring complex backend systems, designing domain layers with entities/value objects/aggregates,… - 90spring-boot-architecture
Use this skill when the user says 'Spring Boot structure', 'Spring architecture', 'Spring Boot clean arch', 'Spring layered', 'Spring hexagonal', 'Spring WebFlux', 'Spring Data', 'Spring Boot folder', or when building a Spring Boot application. This skill enforces:… - 100clean-architecture
Provides implementation patterns for Clean Architecture, Hexagonal Architecture (Ports & Adapters), and Domain-Driven Design in Java 21+ Spring Boot 3.5+ applications. Use when structuring layered architectures, separating domain logic from frameworks, implementing ports and… - 100hexagonal
Expert Hexagonal Architecture (Ports and Adapters) assistance covering driving/driven ports, infrastructure adapters, domain core isolation, and test stubbing. Use when decoupling domain logic from databases/HTTP frameworks, improving testability, or modernizing enterprise applications.