Quick revision
The short answer
Package by layer groups classes by technical role, with controller, service and repository packages that each hold classes for every feature. Package by feature groups them by business capability, so com.acme.orders holds OrderController, OrderService and OrderRepository together. Package by feature keeps related code in one place, so a feature change touches one package, lets classes be package-private to hide internals from other features, and maps naturally onto modules or services later.
What to remember
In this answer, we compared package by layer, where controller, service and repository packages each hold every feature's classes, with package by feature, where com.acme.orders holds the order controller, service and repository together. Package by feature puts code that changes together in one place, so a feature change touches one package, lets us hide internals with package-private visibility that the compiler enforces, and turns each feature into a ready-made unit for a module or service later.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Package by layer groups classes by technical role, with controller, service and repository packages that each hold classes for every feature. Package by feature groups them by business capability, so com.acme.orders holds OrderController, OrderService and OrderRepository together. Package by feature keeps related code in one place, so a feature change touches one package, lets classes be package-private to hide internals from other features, and maps naturally onto modules or services later.
Why this question is asked
How we arrange packages sounds like a matter of taste, but it decides how easy a codebase is to navigate, how much of it is public, and how hard it is to split later. Most Spring tutorials use package by layer, so many codebases start that way and only notice the cost once they have a few hundred classes. Interviewers ask this to see whether we understand the trade-off and Java's visibility rules, not just the folder names. In this answer, we will look at both structures side by side, follow a feature change through each, use package-private visibility to hide internals, and see how feature packages prepare for modules or services.
1Package by Layer
In package by layer, the top-level packages are technical roles. All controllers live in one package, all services in another, all repositories in a third. Each package contains classes for every feature of the application.
This is familiar and easy to start with. Its problem appears as the application grows: with 30 features, the service package holds 50 classes about unrelated things, and nothing in the structure tells us which classes belong together.
2Package by Feature
In package by feature, the top-level packages are business capabilities. Everything about orders, its controller, service, repository, entity and DTOs, lives in com.acme.shop.orders.
| Question | By layer | By feature |
|---|---|---|
| Where is the order code? | in 5 packages | in orders |
| Packages touched to add a field to orders | about 4 | 1 |
Can OrderRepository be hidden from invoices? | no, it must be public | yes, package-private |
| What does the top level tell a newcomer? | this is a Spring app | this app handles orders, customers, invoices, products |
| Path to modules or services | re-sort every class | one package becomes one module |
Notice that the layers have not disappeared. Inside orders there is still a controller, a service and a repository with the same responsibilities. What changed is which grouping comes first: the business boundary is now the outer one, and the technical layering is inside it.
3A Feature Change in Each Structure
Suppose we add a delivery note field to orders. In package by layer, we change OrderRequest in dto, Order in model, OrderService in service, perhaps OrderController in controller and a query in repository. That is four or five packages, each full of unrelated classes, and the pull request is spread across the tree.
In package by feature, every one of those files is in orders. The change is easy to review, and it is obvious that it does not affect invoices or products. The reason this matters is that code which changes together should live together; we change features far more often than we change the idea of a controller.
4Hiding Internals With Package-Private Visibility
Java has four visibility levels. A class or method without any modifier is package-private: visible only to code in the same package. In package by layer this is almost useless, because the controller and the service it calls are in different packages, so nearly everything must be public, and any feature can use any other feature's repository.
In package by feature, only the classes other features genuinely need have to be public. Everything else can be package-private.
Spring has no trouble with package-private classes: component scanning finds them and injection works, because Spring creates beans with reflection. So the compiler now enforces that invoices goes through OrderService and cannot reach into orders' repository. There is one catch: Java packages are not hierarchical, so a subpackage such as orders.internal cannot see package-private classes of orders. That is why many teams keep each feature in a single flat package until it is large, or use tools like Spring Modulith to enforce subpackage rules.
5Ready for Modules or Services Later
Because a feature package already holds everything about one capability and exposes a small public API, it maps naturally onto a larger boundary later. It can become a Spring Modulith module, a Maven module with its own API artifact, or, if there is a real reason, a separate service. With package by layer, the same step requires first finding and re-sorting every class that belongs to the feature, which is the hard part of any extraction.
6Follow-up questions
Does package by feature mean giving up layers?
No. The layers move inside each feature package. A large feature can even have small subpackages such as web and persistence, though that costs some package-private visibility. The key change is that the business capability is the top-level grouping.
Where do shared things like security config or error handling go?
In a small number of clearly named technical packages, such as config or web.errors, at the top level. They should be genuinely cross-cutting; if a class is only used by orders, it belongs in orders. A large common or util package is usually a sign of misplaced feature code.
When is package by layer acceptable?
For a small service with only a few features and a few dozen classes, the difference is minor, and the familiar layer layout is fine. The benefits of package by feature grow with the number of features and the number of people working in the codebase.
7Conclusion
In this answer, we compared package by layer, where controller, service and repository packages each hold every feature's classes, with package by feature, where com.acme.orders holds the order controller, service and repository together. Package by feature puts code that changes together in one place, so a feature change touches one package, lets us hide internals with package-private visibility that the compiler enforces, and turns each feature into a ready-made unit for a module or service later.
Open with
Package by layer organises code by technical role: one package for all controllers, one for all services, one for repositories, each holding classes for every feature. Package by feature organises it by business capability, so com.acme.shop.orders contains OrderController, OrderService, OrderRepository, the Order entity and its DTOs, and the same for customers or invoices.
Then, if they want more
I prefer package by feature for anything beyond a small service, for three reasons. First, code that changes together lives together, so adding a field to orders touches one package instead of four or five, and reviews are easier. Second, it makes package-private visibility useful.
In package by layer nearly everything must be public because the controller and service are in different packages, so any feature can call any other feature's repository. With feature packages, the controller and repository can be package-private, and only the service that other features should call is public, so the compiler stops invoices from reaching into orders' repository.
Spring handles package-private beans fine. Third, a feature package with a small public API maps naturally to a module or even a service later, whereas with layers we would first have to find and regroup every class of the feature. The layers do not disappear; they just live inside each feature.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare package by layer with package by feature in a Spring codebase. Show an example...": https://scrivio-interview-library.netlify.app/java/application-architecture/b529576b83/