Designing Hexagonal Architecture With Java Pdf [work] May 2026
In traditional layered architecture: Web → Service → Repository → Database
class CreateProductServiceTest @Test void shouldSaveProduct() ProductRepository mockRepo = mock(ProductRepository.class); var service = new CreateProductService(mockRepo); var command = new CreateProductCommand("Mouse", 25, "USD"); Product result = service.execute(command); verify(mockRepo).save(any(Product.class)); assertEquals("Mouse", result.name());
private Product toDomain(ProductJpaEntity entity) ... designing hexagonal architecture with java pdf
// Note: ProductJpaEntity is a JPA-annotated class that the domain never sees. // bootstrap/Application.java @SpringBootApplication public class Application public static void main(String[] args) SpringApplication.run(Application.class, args); // Manual wiring if not using Spring's @Autowired on fields @Bean public CreateProductService createProductService(ProductRepository repo) return new CreateProductService(repo);
public interface ProductRepository Optional<Product> findById(String id); void save(Product product); In traditional layered architecture: Web → Service →
Driving Adapter (REST) → Incoming Port (Interface) → Application Service → Outgoing Port (Interface) ← Driven Adapter (JPA)
@RequiredArgsConstructor public class CreateProductService implements CreateProductUseCase private final ProductRepository productRepository; // depends on outgoing port var service = new CreateProductService(mockRepo)
// application/port/in/CreateProductUseCase.java (Incoming Port) package com.example.application.port.in; import com.example.domain.model.Product; public interface CreateProductUseCase { Product execute(CreateProductCommand command);