DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Trending

  • One Query, Four GPUs: Tracing a Distributed Training Stall Across Nodes
  • Why SAP S/4HANA Landscape Design Impacts Cloud TCO More Than Compute Costs
  • Securing Everything: Mapping the Right Identity and Access Protocol (OIDC, OAuth2, and SAML) to the Right Identity
  • LLM Agents and Getting Started with Them
  1. DZone
  2. Coding
  3. Frameworks
  4. Package by Layer for Spring Projects Is Obsolete

Package by Layer for Spring Projects Is Obsolete

Let's talk about packaging and discuss how package by feature and package by layer compare, how the rise of microservices has impacted it, and what to use.

By 
Lubos Krnac user avatar
Lubos Krnac
·
Feb. 26, 18 · Opinion
Likes (58)
Comment
Save
Tweet
Share
32.6K Views

Join the DZone community and get the full member experience.

Join For Free

I believe Spring applications shouldn’t be structured in a package by layer approach. In my opinion, package by feature makes much more sense.

First of all, let me describe each approach briefly.

“Package by Layer” (“Folder by Type” in the Non-Java World)

This project structure groups source code files into packages/directories based on the architecture layer they belong to:

.
└── net
    └── lkrnac
        └── blog
            ├── Application.java
            ├── persistence
            │   ├── ProjectRepository.java
            │   └── UserRepository.java
            ├── dto
            │   ├── ProjectDto.java
            │   └── UserDto.java
            ├── model
            │   ├── Project.java
            │   └── User.java
            ├── service
            │   ├── ProjectService.java
            │   └── UserService.java
            └── web
                ├── ProjectController.java
                └── UserController.java


“Package by Feature” (“Folder by Feature” in Non-Java World)

This approach, on the other hand, groups together files belonging to a certain feature within the system:

.
└── net
    └── lkrnac
        └── blog
            ├── Application.java
            ├── project
            │   ├── ProjectController.java
            │   ├── ProjectDto.java
            │   ├── Project.java
            │   ├── ProjectRepository.java
            │   └── ProjectService.java
            └── user
                ├── UserController.java
                ├── UserDto.java
                ├── User.java
                ├── UserRepository.java
                └── UserService.java


Trends

This subject interested me for a long time. When I Google “package by layer” vs. “package by feature” or “folder by type” vs. “folder by feature”, there seems to be a growing camp of proponents of the “by feature” structure. I am in this camp, too.

Angular (one of the most prominent Single Page Application frameworks) is promoting such folder structure in their style guide.

Spring Project Structure

As there is plenty of reading about pros and cons of each approach out there, I will focus on the implications for Spring projects.

The traditional structure of laying down Spring CRUD applications (if your back-end application is not using Spring Data REST) is divided into three layers: web/service/persistence. The vast majority of Java/Spring projects I was working on followed this structure.

Coupling

Package by layer most probably originates in the previous century, where layered architectures were used as a decoupling mechanism. In fact, “decoupling” was often the answer when I was challenging package by layer structures. I disagree. To me, package by layer is one of the major reasons behind tight coupling.

When you are writing a signature for classes in a package by layer structured project, what keyword is first? I bet it is public. Does the public access modifier help decoupling? I guess nobody would answer yes to this question.

Then why are developers using public access modifier all over the place? It is exactly because the project is structured in a by-layer fashion. The repository class needs to be public because it needs to be accessed from the service package, and the service needs to be public because it needs to be accessed from the web package. When everything is public, it is very hard to maintain discipline to avoid a big ball of mud.

When using package by feature, package private UserRepository (meaning no access modifier is specified) can’t be used by services other than UserService, because they are in the same package. And if we decide that only UserController should use UserService, we just make it package private, because they share same package. In such a project structure, most of the classes would be package private. Therefore developer should have a very good reason to make the class public.

Scaling

What happens if a project starts to have 10+ classes in the web/service/persistence layer? Developers tend to group classes into sub-packages. But how do they categorize them? From my experience, it is mostly based on features. So we can often find such structure in bigger projects:

.
└── net
    └── lkrnac
        └── blog
            ├── Application.java
            ├── dao
            │   ├── ...other repositories...
            │   ├── ProjectRepository.java
            │   └── user
            │       ├── UserRepository.java
            │       └── UserRoleRepository.java
            ├── dto
            │   ├── ...other DTOs...
            │   ├── ProjectDto.java
            │   └── user
            │       ├── UserDto.java
            │       └── UserRoleDto.java
            ├── model
            │   ├── ...other models...
            │   ├── Project.java
            │   └── user
            │       ├── User.java
            │       └── UserRole.java
            ├── service
            │   ├── ...other services...
            │   ├── ProjectService.java
            │   └── user
            │       ├── UserRoleService.java
            │       └── UserService.java
            └── web
                ├── ...other controllers...
                ├── ProjectController.java
                └── user
                    ├── UserController.java
                    └── UserRoleController.java


Isn’t this obvious madness?

Future Proof-ness

As a bunch of smart people suggest, it might not be a good idea to start greenfield project with a microservices architecture. I agree. So it might be a good idea to prepare your monolith for eventual separation into smaller projects if your application hits major growth.

Imagine extracting microservices from your monolith project. Or splitting the whole project into microservices. I hope everybody understands that no sane microservices architecture is separated by architectural layers. Separation based on features is used. So which project structure will be easier to separate into microservices? The one where any public class can use any other public class from any package (package by layer)? Or one separated into package private buckets (package by feature)? I believe the answer is obvious.

Conclusion

Package by feature is a simple but very powerful mechanism for decoupling. So next time some layer-obsessed developer defends the package by layer project structure as a decoupling mechanism, please correct her/his misunderstanding. I believe dinosaur-ness is the only reason why package by layer still exists nowadays.

Spring Framework

Published at DZone with permission of Lubos Krnac. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook