Package & Class Location Rules
About
Package & Class Location Rules define how classes should be organized within the project’s package structure. Proper organization improves readability, maintainability, and modularity.
In Spring projects, packages often reflect functional or architectural separation, such as controller
, service
, repository
, entity
, dto
, mapper
, and util
. Enforcing these rules ensures that:
Classes are predictably placed based on their role.
Developers can quickly locate relevant classes.
Tools like ArchUnit or static analyzers can validate package conventions.
These rules complement layered architecture rules by providing physical structure guidance, ensuring that classes not only follow proper dependencies but also reside in the correct locations within the project.
Purpose
The purpose of Package & Class Location Rules is to enforce consistent placement of classes within the project structure, ensuring that the package organization reflects the role and responsibility of each class.
Key objectives include:
Improve project readability and navigation
Developers can easily find classes based on package names, reducing time spent searching for code.
Support maintainability and modularity
By keeping related classes together and separating unrelated components, the codebase becomes easier to maintain and extend.
Enable automated validation
Tools like ArchUnit can verify that classes are correctly placed, reducing errors introduced during development or refactoring.
Reinforce architectural rules
Proper package placement complements layered architecture rules, ensuring that dependencies align with physical structure.
Facilitate team collaboration
A predictable structure ensures that multiple developers working on the same project follow the same conventions, making code reviews and onboarding smoother.
Rules
Consider the following packaging structure
com.company.employeeportal
├── config # Spring and application configuration (DataSource, Swagger, CORS, etc.)
├── client # External API calls logic
├── constants # Application-wide constants and enums
├── api # REST controllers for handling HTTP requests
├── dto # Data Transfer Objects for request/response bodies
├── entity # JPA entity classes (Employee, Department, Salary, etc.)
├── exception # Custom exceptions and global exception handling
├── mapper # MapStruct or manual mappers (Entity <-> DTO)
├── repository # Spring Data JPA repositories specification
├── specification # Spring Data JPA specification
├── service # Service classes for handling business logic
├── util # Utility/helper classes (DateUtils, PaginationUtils, etc.)
├── validation # Custom validators and annotation-based rules
1. Controller Layer (api
)
api
)Classes annotated with
@RestController
or containingController
in their name must reside in thecontroller
package.Controllers must not be placed in
service
,repository
, orentity
packages.
2. Service Layer (service
)
service
)All business logic classes should reside in the
service
package.Services must not be placed in
controller
,repository
, orentity
packages.
3. Repository Layer (repository
, specification
)
repository
, specification
)Repository interfaces and Spring Data JPA specifications must reside in
repository
orspecification
packages.Repositories must not reside in
service
,controller
, orentity
packages.
4. Entity Layer (entity
)
entity
)JPA entity classes must reside in the
entity
package.Entities must not be placed in
controller
,service
, orrepository
packages.
5. DTO Layer (dto
)
dto
)DTOs for requests and responses must reside in the
dto
package.DTOs must not be placed in
service
,repository
, orentity
packages.
6. Mapper Layer (mapper
)
mapper
)Mapper classes should reside in the
mapper
package.
7. Client Layer (client
)
client
)Classes handling external API calls must reside in the
client
package.Clients must not reside in
service
,controller
, orrepository
.
8. Validation Layer (validation
)
validation
)Custom validators must reside in the
validation
package.
9. Utility Layer (util
)
util
)Generic helper classes must reside in the
util
package.
10. Exception Layer (exception
)
exception
)Custom exceptions and global exception handlers must reside in the
exception
package.Exceptions must not reside in service, controller, or repository packages.
11. Config Layer (config
)
config
)Spring or application configuration classes must reside in the
config
package.
Last updated