Spring Boot is built on top of the conventional spring framework. So, it provides all the features of spring and is yet easier to use than spring. In this article, we are going to see how one should start and structure his Spring Boot application.
- Good knowledge of Java.
- Basic knowledge about Spring Boot.
- Basic knowledge about Creating REST API with SpringBoot.
You may have worked with Spring Boot and many times become confused about how to start and structure your projects so in this article we are going to see how you can start and structure your project in a manner so that others can also read and understand your project easily.
Here we are going to structure a simple application in spring boot and we’ll see how can we implement that.
Step By Step Procedure:
Step 1: First go to spring initializer and create a new project using the following information
- Project: Maven
- Language: Java
- Spring Boot: 3.0.0
- Packaging: JAR
- Java: 8
- Dependencies: Spring Web, Spring Data JPA, H2 Database
Click on Generate which will download the starter project.
Step 2: Now extract the given folder and then open this project in your preferred IDE, I’ll use IntelliJ Idea Community edition for that, To open this just click on open and then select the extracted folder from your files.
Opening project in IntelliJ Idea
After opening the Project you’ll see the following screen
Step 3: Now we’ll structure our project and for that, we are going to create our packages we’ll make sure we create packages and use them to make our file tree better to read and interpret. We’ll mostly use the following packages and create classes and interfaces in these packages :
- Controller: It will contain all classes and interfaces related to controllers.
- Repository: It will contain all the repositories-related interfaces and classes.
- Service: It will contain all the business logic-related interfaces and classes.
- Model: It will contain all the models in form of classes.
- Exceptions: It will contain all the custom exceptions
To create a package go to the following folder -> src > main > java > com.example.demo. Now right-click on this folder > new > package > give name > press enter
Creating new package
Step 4: Create 4 more packages and we’ll get the following structure for our packages
File tree for our current project
This is going to be the project structure that we are going to use also depending on your use case you can create more packages and create classes inside them. Example – Security
Step 5: Now we can create classes inside all the packages depending upon the requirements and can create the best way so that any other developer can manage your project easily.
Share your thoughts in the comments
Please Login to comment...