How do you keep a bigger Spring Boot application modular without creating more and more layers?
How do you keep a bigger Spring Boot application modular without creating more and more layers? I was watching Jakub Nabrdalik talks about package scope, modularity and hexagonal architecture and it made me think again about how we structure bigger Spring Boot applications. A lot of projects start with something like: text controller/ service/ repository/ entity/ configuration/ It is simple at the beginning. But after few years you can have 100 services, 80 repositories and a lot of classes which are public only because this is how the project was created. The package structure tells you almost nothing about the business. I started to prefer something closer to this: ```text orders/ OrderFacade.java // public CreateOrder.java // public OrderResult.java // public Order.java // package-private OrderItem.java // package-private OrderService.java // package-private OrderRepository.java // package-private OrderPolicy.java // package-private OrderConfiguration.java // package-private ``` From another module I want to see mostly: text OrderFacade CreateOrder OrderResult and not all implementation classes. For me this gives public a real meaning. public means that this is part of the API of my module, not only that Spring can instantiate it. Java package scope is actually quite powerful for this. Infrastructure The same idea can be used for infrastructure. For example the business module can define the port: ```java interface OrderRepository { Order find(OrderId id); void save(Order order); } ``` and infrastructure can provide the implementation: ```text orders/ OrderRepository.java infrastructure/ persistence/ HibernateOrderRepository.java JpaOrderEntity.java SpringDataOrderRepository.java ``` The infrastructure classes don't have to become part of the API used by the rest of application. I think this is one nice thing about Ports and Adapters. The domain says what it needs. Hibernate, Kafka, HTTP clients etc. are implementation details. It also helps to avoid designing th