@RequiredArgsConstructor public class CreateProductService implements CreateProductUseCase private final ProductRepository productRepository; // depends on outgoing port
Driving Adapter (REST) β Incoming Port (Interface) β Application Service β Outgoing Port (Interface) β Driven Adapter (JPA)
@Override public Optional<Product> findById(String id) return jpaRepository.findById(id).map(this::toDomain); designing hexagonal architecture with java pdf
public interface ProductRepository Optional<Product> findById(String id); void save(Product product);
@Override public Product execute(CreateProductCommand command) var id = UUID.randomUUID().toString(); var money = new Money(command.price(), command.currency()); var product = new Product(id, command.name(), money); productRepository.save(product); return product; public interface ProductRepository Optional<
// 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);
@SpringBootTest @Testcontainers class JpaProductRepositoryTest @Container static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15"); @Test void shouldSaveAndRetrieve() // ... real database test void save(Product product)
// 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);