Architecting for Success: A Guided Tour of Clean Architecture in Spring Boot with Steve Pember
This article provides a comprehensive companion to Steve Pember’s insightful talk at Spring I/O 2023 on building Spring Boot applications with clean architecture. We’ll follow Steve’s journey as he explores the importance of software architecture, delves into the principles of clean architecture, and demonstrates how to effectively implement these principles within the Spring Boot ecosystem.
Main Ideas
- Why Architecture Matters: Good architecture is an investment. It might slow you down at first, but it pays off by making your code easier to maintain, test, and change in the long run. Think of it like building a house with a strong foundation – it takes more time upfront, but it prevents problems down the road.
- Clean Architecture Principles: Steve breaks down clean architecture into four main parts:
- SOLID principles (guidelines for writing good code)
- Component principles (breaking your system into independent parts)
- Boundaries and dependencies (keeping things separate and pointing dependencies inwards)
- Details (delaying decisions about things like your database)
- Clean Architecture in Spring Boot: Spring Boot makes it easy to do clean architecture with modules. Steve suggests having at least three: core (for your business logic), details (for databases and other external stuff), and app (for Spring configuration).
- Unpopular Opinions: Steve disagrees with some common practices. He says JPA entities shouldn’t be in your core module, and that you should do lots of integration testing. He argues that these practices can make your code harder to change and maintain in the long run.
General Themes
- Maintainability: Good architecture helps keep your code from becoming a tangled mess, like a well-organized closet versus a pile of clothes on the floor.
- Testability: Clean architecture makes it easier to test your code because each part can be tested separately.
- Flexibility: A well-architected system is easier to change when your needs change, like having adjustable shelves in that closet.
- Discipline: Sticking to the rules of clean architecture takes discipline, but it’s worth it in the end.
Key Excerpts
- “Dependencies should always point inward to the core.” 14:58 This means that the core of your application should not depend on anything else, but other parts of the application can depend on the core.
- “The database is not important, not really. It’s just a mechanism for storage.” 30:17 Steve emphasizes that you shouldn’t get bogged down in database details early on. Focus on the core logic first.
- “Your environment is a detail. Your code must not know or care where it is being run.” 30:54 Your code should work the same whether it’s running on your laptop or in the cloud.
- “Spring too is just a detail.” 32:46 Don’t let Spring specifics leak into your core business logic.
- “A good suite of integration tests which cover your apps efficiently is like a warm safe cozy blanket.” 26:58 Integration tests give you confidence that your whole application works together correctly.
- “The outer layers may only see what’s happening in the inner layers. The inner layers have no idea about the outside.” 24:56 This is how you keep your core business logic independent from things like the user interface or database.
- “If you do it right, your inner layers are immune to changes in your outer layers.” 25:19 This is a major benefit of clean architecture – changes in one part of the application are less likely to break other parts.
What is Software Architecture? 3:16
Steve starts by discussing what software architecture really means. He argues that it’s more than just drawing boxes and lines – it’s about understanding every part of your system and how it all fits together. He compares it to designing a building, where the architect needs to consider everything from the overall structure down to the placement of electrical outlets.
“A good software architect should be able to look at, uh, think at, and design at every level of the stack, from a microservices topology all the way down to a SQL query or whatever.” 4:32
Why Should We Care About Architecture? 4:54
Steve explains why good architecture is essential for building successful software. He argues that while features and speed are important in the short term, neglecting architecture can lead to problems later on. If your codebase becomes a tangled mess (“spaghetti code”), it becomes difficult and expensive to make changes or add new features.
“Without guardrails, without structure, eventually the system becomes brittle, loose, uh, easy to break, too hard to maintain.” 6:32
He highlights the benefits of a well-architected system, such as:
- Separation of Concerns: Each part of the system has a clear responsibility, making it easier to understand and work with.
- Modularity: The system is broken down into smaller, independent modules, which can be developed and tested separately.
- Decoupling: Different parts of the system are not tightly coupled, so changes in one area are less likely to affect others.
- Testability: It’s easier to write automated tests for a well-architected system.
- Maintainability: Perhaps the most important benefit, maintainability means your system is easier to change and update over time.
“Having a well-architected bit of software offers quite a few benefits… separation of concerns, highly modular, decoupled systems, which are flexible, highly testable, and arguably most important, this system is far far easier to maintain than spaghetti.” 7:25
Clean Architecture: The Core Principles 9:22
Steve introduces clean architecture as a way to achieve the benefits mentioned above. He explains that clean architecture is all about keeping your core business logic separate from “details” like the database, user interface, or external services. This makes your code more independent and less likely to break when those details change.
“If you ask me to summarize clean architecture… I would break it down into four high-level concepts: the notion of being SOLID, component design, boundaries and their dependencies upon each other, and something we’ll get into later, details.” 16:26
He describes the four key concepts of clean architecture:
- SOLID Principles: These are five principles for writing good object-oriented code: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion.
- Component Principles: Components are independent units of code that can be developed and tested in isolation.
- Boundaries and Dependencies: Clean architecture emphasizes having clear boundaries between components and ensuring that dependencies point inwards towards the core domain.
- Details: Things like the database, user interface, and external services are considered “details” that should not affect the core business logic.
“If there’s one thing you should take away from this talk, whatever else I say, is dependencies should always point inward to the core.” 14:55
Implementing Clean Architecture in Spring Boot 32:56
Steve explains how to put clean architecture into practice using Spring Boot. He suggests organizing your project into multiple modules:
- Core Module: Contains the core business logic, domain entities, and use cases (services).
- Details Module: Contains implementations for things like database access, external service calls, etc.
- App Module: Contains the main application class and Spring Boot configuration.
“Consider architecting [your application] as a multi-module or sub-project code base… At a minimum, I suggest three modules, and they are: core, details, and app.” 33:47
He also discusses specific techniques for implementing clean architecture in Spring Boot, such as using visibility modifiers to hide implementation details, keeping controllers minimal, using Spring Integration for messaging, and writing comprehensive integration tests.
“Having a repeatable set of integration tests that cover my application, again, warm blanket, it’s wonderful. I don’t care what the book says, it’s great, and it’s so easy to do this… JVM, Spring, test containers, you know, my integration suite runs my application, it launches all of my containers.” 42:35
Challenging Common Practices 43:55
Steve challenges some common practices in Spring Boot development that can violate clean architecture principles. For example, he argues against putting JPA entities in the core module because it couples the domain model to the database. He also reminds the audience that even Spring itself is an implementation detail and should not be tightly coupled with the core business logic.
“This last bit may seem horrible to any Spring JPA fans… one side effect of following these rules is that you should have no entity classes in your core module.” 43:56
Embracing Practicality and Conclusion 48:46
Steve acknowledges that clean architecture can sometimes require writing more code, but he argues that the benefits in terms of maintainability and flexibility are worth it. He encourages developers to be disciplined in following the principles of clean architecture, even when it seems easier to take shortcuts.
“You need this discipline, you need to follow these rules. Having these dependency separations keeps your code isolated, keeps it encapsulated.” 49:00
He concludes by emphasizing that clean architecture is a valuable tool for building robust and adaptable Spring Boot applications. By following these principles, developers can create systems that are easier to maintain, test, and change over time.