DEV Community

Atul Kumar PK
Atul Kumar PK

Posted on

Spring boot codebase structure

Hi guys,
This is my first post here. I have been working with spring boot for years. I would like to learn the best practice for structuring a spring boot project's codebase.

It would be awesome if you guys could help and I really appreciate it.

Top comments (2)

Collapse
 
ndrone profile image
Nicholas Drone

This could become very opinionated. But for my experience. I don't use the anonations of @Autowired, or and of the component scanning annonations such as @Component or @Service. All beans are created from @Configuration class and imported on the @SpingBootApplication. This also for building of beans via the constructor which makes them easy to test without spring in the test cases. It also provides more context around what is happening when spring boot is starting for those young developers to follow that are not familiar with spring boot.

Collapse
 
ndrone profile image
Nicholas Drone

As far as project structure. I start with 2 modules Applications and components.

Applications are your microservices. They contain your spring boot dependencies, controllers and message listener code. The idea is they take the information off the wire and passes it onto your service layer which is in your components module. This way if the delivery protocol changes you only need to change your application layer code.

Components are broken out into multiple modules. I usually have one or more core module. The core module contains all or most of my interfaces, not the implementations, any pojos needed for the interfaces, helper classes and utils. These are split on domains depending on project size.

Repository implementations are all separate by domain and technology. For example, a contact domain may have 2 modules one for JDBC and one for HTTP.

Then I have a service module for the repositories which is what the applications interface with if need be. If the service layer is just a pass-through then I forgo the service layer.