Spring Boot Code Structure: Package by Layer vs Package by Feature

  Рет қаралды 24,928

Dan Vega

Dan Vega

Күн бұрын

In this tutorial we will take a look at how to structure your code in your Spring Boot applications. First we will cover some best practices or things you should avoid. After that we will cover 2 ways to structure you code and what the pros and cons of each approach might be.
🔗Resources & Links mentioned in this video:
Components vs Beans: • Spring Beans Showdown:...
GitHub Repository: github.com/dan...
👋🏻Connect with me:
Website: www.danvega.dev
Twitter: / therealdanvega
Github: github.com/dan...
LinkedIn: / danvega
Newsletter: www.danvega.de...
SUBSCRIBE TO MY CHANNEL: bit.ly/2re4GH0 ❤️

Пікірлер: 59
@benderbg
@benderbg Жыл бұрын
For a total beginner this video is packed with useful information. Thanks for explaining it in Layman's terms.
@DanVega
@DanVega Жыл бұрын
So glad that you found this useful, thank you!
@JohnTriantafillakis
@JohnTriantafillakis Жыл бұрын
Dan, it would be great a demo with a comparison of Hexagonal architecture ,Layer, Package
@DanVega
@DanVega Жыл бұрын
We are working towards that...one step at a time.
@praveens2272
@praveens2272 Жыл бұрын
Hexagonal architecture just makes unnecessary complexity to your project.
@BingyangWei
@BingyangWei Жыл бұрын
In my latest Spring Boot 3 tutorial, I adopted the "Package by Feature" approach. In my old Spring Boot 2 tutorial, for the same running example, I used the "Package by Layer" approach. If you are interested, please take a look. Feel free to provide any feedback.
@ismile47
@ismile47 11 ай бұрын
@DanVega waiting on the above request. Just a reminder. That will be really helpful for community as there is so much of confusion around this concepts which and how to use.
@fredericolivier7896
@fredericolivier7896 Жыл бұрын
I do package by feature 100% of the time.
@PeterSarazin
@PeterSarazin 2 ай бұрын
What is the best way to structure junit tests with package by feature?
@cbmeeks
@cbmeeks Жыл бұрын
I packaged by layer for years. Until I saw some people packaging by feature and I thought that was a crazy idea. Until I tried it myself. I found package by feature is very nice for large projects. No going back for me.
@DanVega
@DanVega Жыл бұрын
Thanks for sharing!
@_leftii
@_leftii Жыл бұрын
Love it! I was iffy about posing this question in office hours a few months back, but after seeing how much engagement there is on this video I'm so glad I did and am not the only one that was interested!
@FahadAli-km3ll
@FahadAli-km3ll Жыл бұрын
Hi Dan. Thank you for another great video. I clearly see the benefits of the package by feature approach. However, I am wondering what your opinion is on implementing relationships between the entities. For low coupling I imagine that every package would only have one single point of entry for other packages (e.g. a service or interface exposed to be used by other features). In which case how would you go about designing the relationships between the entities? You ofc won't be able to leverage the Spring Data JPA annotations so I guess you would create DTOs between the features (which, in my opinion, may introduce unnecessary complexity depending on the size of the project). And maybe just storing the id of the referenced entity? Is some leakage acceptable? Let me hear your opinion, thank you! :)
@tonydeveloperdndndn
@tonydeveloperdndndn Жыл бұрын
Short video but proving the good overview. Thanks for that.
@DanVega
@DanVega Жыл бұрын
Glad it helped
@Anveshana837
@Anveshana837 12 күн бұрын
Dan, thanks for the video, it was helpful.
@darkogrozdanovski
@darkogrozdanovski 9 ай бұрын
I generally work by packaging by feature and then layers within as you mentioned at the end. In my code each feature module stays quite compact but the additional layer packages give me a bit of structure and make it easier to navigate the code. Its usually up to 20 files per feature, 2-3 services, 7-10 use case interfaces, entities, DTO-s for Inputs and outputs, filtering etc.. I think the boundaries between features are clear and not an issue at all even though i lose the package visibility benefit of packaging by feature in a flat package. What i really struggle with is having Data JPA relationships having references to other feature entities. I feel like Data JPA is not well suited for this way of structuring code, or I am not using it correctly. So as an example in sports, having a "Matches" Feature, and a "Teams" feature, where a match references a team, but a team is also standalone. So if i just use teamId in match its really hard to query (lets say i want to get a list of matches filtered by a specific team), so i have a JPA OnetoMany relationship there.
@Sunil_Veeramuthu
@Sunil_Veeramuthu Жыл бұрын
Thanks, Dan - this is very insightful as always! Aside from just sounding right, generally speaking, the biggest upside for me is the ease with which you can fork out a microservice when packaging by feature (and maybe modularity to a certain extent). That is a good litmus test on how cohesive and less coupled your code is. But then, you don’t create new projects every day, so you’re basically stuck with whatever the original authors intended, and that’s usually packaging by layer.
@static-m-s
@static-m-s Жыл бұрын
Great article/video! Thank you!
@DanVega
@DanVega Жыл бұрын
Thank you!
@sarkar_957
@sarkar_957 Жыл бұрын
Killing it... always love to watch your videos 😉
@grenadespoon
@grenadespoon Жыл бұрын
In package by feature, would you put a security config into a config package, or a security package? What about a controller advice?
@RobsonCassianoSoftware
@RobsonCassianoSoftware Жыл бұрын
I put them of a package called shared/configs/SecurityConfig
@houn-x
@houn-x Жыл бұрын
This was very useful. You've just gained a new subscriber in me!
@USONOFAV
@USONOFAV Жыл бұрын
If you doing monolithic apps package feature is more maintainable for microservices package by layer.
@JimShingler
@JimShingler Жыл бұрын
Really great explaination!
@cviniciusm
@cviniciusm Жыл бұрын
I think your article lacks the "comment" implementation to make clear the boundaries of layer vs feature, to show us how comment and post will relate to each other.
@DanVega
@DanVega Жыл бұрын
Agree that can also be a discussion on DDD
@mrngwozdz
@mrngwozdz Жыл бұрын
I am mixing both types. I found that keeping packages such as: configuration, entities, controller, dtos, and mappers as "Package by Layer" works well, but I organize all services in the following manner: services ---- product -------- business In this package, you can find our main code. Everything we have to write manually, come up with, etc., is because it's business-related. This package should also be tested 100% by unit tests. -------- data Here we have a package where we can find all the classes from which we obtain data for a product. We can source this data from databases, external services, etc. There is no logic; the primary responsibility is simply to retrieve raw data from external sources. I find this structure the most readable. I disliked searching for controllers or entities using "Package by Feature". On the other hand, writing clean code with tons of business classes in "Package by Layer" is not possible, or at least I've never seen it done.
@scerrisebastien
@scerrisebastien Жыл бұрын
Very interesting discussion indeed. If a service A need a service B datas to build a response, how you handle this in feature design. You need to keep public accessor, is n it ?
@danieldiaz6025
@danieldiaz6025 Жыл бұрын
I generally prefer package-by-feature. But: I also like API-first development (generating controller interfaces using OpenAPI Generator for example) and I feel it clashes a little with the package-by-feature approach, because you generate an entire "layer" (the controllers) in one go, and all the results tend to be put in the same package. Is there a solution to this apparent tension?
@davidleonardobernal61
@davidleonardobernal61 Жыл бұрын
X2
@ashishsengar87
@ashishsengar87 Жыл бұрын
Great video
@asterixcode
@asterixcode Жыл бұрын
what about a package strucuture with 4 main packages: api (containing other packages such as controller, mappers/assemblers, listeners), domain, core, infrastructure? Then the tradicional layer package within domain, core and infra?
@DavidSoles
@DavidSoles Жыл бұрын
Good video. Thanks for making such useful videos
@DanVega
@DanVega Жыл бұрын
Thank you for watching them David!
@kacpergrabiec
@kacpergrabiec Жыл бұрын
What do you think about multi-module approach?
@ChristopherWarmbold
@ChristopherWarmbold Жыл бұрын
@DanVega Could you also make a video comparing java packages vs maven modules when structuring your application
@tav1119
@tav1119 Жыл бұрын
the only advantages of having maven modules are if you have different teams taking care of the same software. Usually a maven module project is bundled to only one jar or war.
@DHA508
@DHA508 Жыл бұрын
Thanks Dan
@davidleonardobernal61
@davidleonardobernal61 Жыл бұрын
Hi Dan, In a scenario where the API-First methodology is employed, and we have an openapi.yml file representing all the operations of our API (in this case, the blog API), I understand that in the case of a layered architecture, we would typically have a BlogController where we implement all our methods. However, in the case of adopting a feature-based architecture, would we create a separate openapi file for each feature, where the specific functionality is implemented? Or would we follow a similar approach as in the layered architecture by sharing this interface among all the features in some way?
@ogyct
@ogyct 7 ай бұрын
This all makes sense until we begin doing something more complicated. Let's say we have Authors, who write the posts. Naturally we want our entites to have relations. This way we no longer can make entity classes package-private. We also create coupling. In the end, the whole idea of package by feature becomes not viable. At least not in the way presented in the video. In my experience module will always contain all the stuff, which is related to each other in some way. An we come back again to package by layer. True modularity is possible only on not related stuff. e.g. i18n module for our FE. What do you think about that?
@UditSharma124
@UditSharma124 Жыл бұрын
Great Explanation!! What about Global Exception Handler and Security configurations in package like feature structure?
@BrownBearMY
@BrownBearMY Жыл бұрын
I'd usually store them by according to their scope. For example, I keep Global Exception Handler under web package as it's about translating Exceptions to HTTP status.
@egonsaks
@egonsaks Ай бұрын
How do I get the spring tool on the sidebar so I can see what spring beans are in application context?
@alpsavasdev
@alpsavasdev 4 ай бұрын
I also like this approach the best but I have a question. When I access from a service in one package to another service or a repository etc. in a different package, is that OK? My second question is is there a resource like a book or video series or something similar to learn more about this approach in more details?
@PeterSarazin
@PeterSarazin 2 ай бұрын
What is the best way to structure junit tests with package by feature? Are there any examples of this?
@iishanto
@iishanto 8 ай бұрын
What should be done when a model in a feature package has a relation with other models outside of the feature package, such as when a Post model has a relation with a Comment model? Is there any way to handle that?
@thebest-cr2sb
@thebest-cr2sb 6 ай бұрын
is it possible to do a packaging by layer inside the features , in my case i have a lot of DTOs and and sevral repositories since the feature containe many entities , and i want to know is it correct to split them by layers inside the feature package ?
@danielcommins1285
@danielcommins1285 Жыл бұрын
Coupling between layers can be mitigated by interfaces, no?
@grenadespoon
@grenadespoon Жыл бұрын
Thanks for doing this
@aziztolearn
@aziztolearn Жыл бұрын
Shouldn't we call packages plural like controllers, models? Or that's just up to us to call model or models?
@aryanraj9235
@aryanraj9235 Жыл бұрын
thankyou sir of such contents
@youtuger
@youtuger 5 ай бұрын
i have to try package by feature one day. But i wonder what happens when i have 50 domain objects for different services
@geni4551
@geni4551 Жыл бұрын
Just the GOAT
@DanVega
@DanVega Жыл бұрын
You just mae my day!
@Geniusmayor
@Geniusmayor Жыл бұрын
How will test be written if the classes are not public in the package by feature version?
@majidhajric1871
@majidhajric1871 Жыл бұрын
Thanks for video but too much time spent on explaining package scanning. I have been worked on different projects and can say that if there is a shortcut, good lazy developer will take it. Using any of mentioned approaches without discipline will sooner or later lead to spaghetti code and dependency mix. Personally, hexagonal architecture seems like overhead at the beginning but it puts constraints and at the end there is benefit from it. All the best!
@buksvdl
@buksvdl Жыл бұрын
Code explosion. All classes except for the Service differ(almost) only in name. Should they even exist?
Accompanying my daughter to practice dance is so annoying #funny #cute#comedy
00:17
Funny daughter's daily life
Рет қаралды 24 МЛН
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 193 МЛН
Noodles Eating Challenge, So Magical! So Much Fun#Funnyfamily #Partygames #Funny
00:33
Vertical Slice Architecture, not Layers!
46:24
CodeOpinion
Рет қаралды 125 М.
the right way to organise your code
17:33
Amigoscode
Рет қаралды 202 М.
Microservices Gone Wrong at DoorDash
17:22
NeetCodeIO
Рет қаралды 162 М.
4 Spring Boot Mistakes to AVOID in 2024
15:18
Java Brains
Рет қаралды 64 М.
Транзакции - Spring Framework в деталях
1:20:21
Уголок сельского джависта
Рет қаралды 27 М.
Spring Boot Testing - ** Batteries Included 🔋🔋
41:12
Dan Vega
Рет қаралды 24 М.
Event-Driven Architecture (EDA) vs Request/Response (RR)
12:00
Confluent
Рет қаралды 172 М.
Accompanying my daughter to practice dance is so annoying #funny #cute#comedy
00:17
Funny daughter's daily life
Рет қаралды 24 МЛН