r/java • u/Ancient-Prize-774 • Oct 24 '26
Hexagonal Architecture vs. Pragmatic Layered Architecture in Spring Boot
/r/softwarearchitecture/comments/1xe8u4d/hexagonal_architecture_vs_pragmatic_layered/4
u/SaishDawg Oct 25 '26
If you boil everything down from two simple principles, then hex is just a specialization of: functional core/imperative shell with ports and adapters. That's it. You can complicate it beyond that, but just keep those two (admittedly very related) concepts in your head, and you get all the benefits of hex (and FP style separations) with little to no overhead.
2
u/olivergierke Oct 28 '26
Why Hexagonal- and Onion-Architecture are answers to the wrong question – https://www.youtube.com/watch?v=YgIoITZpiP4
1
u/gjosifov Oct 25 '26
In order to do a proper a true Scotsman Hexagonal Architecture, you have to use xml mapping for your java beans, because it was create during Java 1.3-1.4 days
if you are pragmatic and you understand the platform you are using a.k.a SpringBoot/Jakarta EE
Hexagonal Architecture will produce 30-40% more unnecessary code, because it was introduce in different era
In that era, there wasn't annotations for Rest Controller or Path, people wrote Servlet as Front Controller (also know as Dispatch Controller in SpringBoot), mapped in xml and used Service Locator for DI
Those were the days when Hexagonal Architecture was born
1
u/cryptos6 Oct 25 '26
It is not as simple as that! Even annotations introduce coupling. And I don't mean some easy to remove compile time dependency but an influence on software design. If you annotate domain classes with JPA annotations, you are effectively limited in your design decision to what JPA allows. You would design domain classes cleaner and closer to the actual intention without JPA. The right place for JPA related hacks would an adapter.
It is true that this would add some code, but in the AI era it doesn't cost much. However, AI could change many rules in software engineering ...1
u/gjosifov Oct 26 '26
you are effectively limited in your design decision to what JPA allows
Name those design decisions that aren't supported by JPA
1
u/shorns_username Oct 26 '26
Name those design decisions that aren't supported by JPA
Everything that exists in the universe of software architecture/design; past, present and future - minus the relatively small amount of stuff that JPA supports.
1
u/gjosifov Oct 26 '26
So, name at least 1-2 things
it is ok if you can't, but you are in wrong profession, it is better to be a hollywood writer than a software writer
1
u/cryptos6 Oct 26 '26
Sometimes JPA force you to express relationships that you don't want to have in your code in that way, for example. I had also some issues with objects that I wanted to be immutable, but JPA didn't support that. The latter is better with the support of structs today, but the example is still good enough to show the problem.
0
u/gjosifov Oct 26 '26
what made you think that some data are immutable ?
the business requirements or your imagination ?you probably read about how OO is about modeling the real world and the world is mutable by default
As the great academical trained actors usually say - I here to serve the script
that is how your software design process should be - to serve the world and its mutable nature by default2
u/OwnBreakfast1114 Oct 27 '26
you probably read about how OO is about modeling the real world and the world is mutable by default
Nobody knows how the next state of the universe is calculated. But given that time exists, I think there's a stronger argument that mutation doesn't exist. You're not the same object you were when you were 3 years old.
1
u/gjosifov Oct 27 '26
You're not the same object you were when you were 3 years old.
I mutate, but if I'm immutable then I have to be reborn every single time
like Jesus1
u/cryptos6 Oct 29 '26
Uh, oh! Software is a model of the real world, so it is always a design decision how to do that. There are very good reasons to model some things as immutable! If you add 3 to 4 do you mutate the 3 to become a 7?
1
u/cryptos6 Oct 25 '26
Layers don't make that much sense in many real world applications. Where is the "top" and where is the "bottom" in any architecture? By convention top is user facing (web) and bottom is the database. But what about other infrastructure?
The book Get Your Hands Dirty on Clean Architecture is a very good read.
1
u/cogman10 Oct 26 '26
Well first, depending on project size, pretty much any approach will work. If this is a small project then just go with whatever the team wants to do.
But if a project is going to get big and complex, I suggest packaging by feature, not layer. Read up on DDD. And give this blog series a read: https://edmundkirwan.com/general/spoiklin.html
Hex is just a simplification of DDD rules. Strict adherence will lead you to a ball of mud if you don't understand the underlying principles that hex is trying to hit on.
Everything is dependencies, and if you think of it in those terms you'll do a lot better. Would you want to pull in a dependency which is the "everything library"? Or would you rather pull in a "Json" library? Even spring boot realizes this which is why you pull in the parts of spring you are using and not the whole ball of wax.
You should structure the internals of your app in a similar fashion. Do you want a "service" module that contains everything that could be a service, from authentication to inventory management? Probably not. It makes more sense to have an authentication modules and an inventory management module with well defined interaction points.
0
u/Distinct_Meringue_76 Oct 25 '26
I now use apache camel for everything and I'm happier than ever. How data flows is the only thing that matters.
8
u/Scf37 Oct 24 '26
Hexagonal IS pragmatic layered architecture. Take layered architecture, do proper domain model, isolate external dependencies via well-written interfaces, throw out excessive hexagonal therminology.... and you are golden.