Spring Boot HandBook

    Creating your first spring boot project#

    Step 1: Start a new Spring Boot project#

    1. Use start.spring.io to create a “web” project. In the “Dependencies” dialog search for and add the “web” dependency as shown in the screenshot. Hit the “Generate” button, download the zip, and unpack it into a folder on your computer. Note :- Choose the latest version of Java and Spring boot

    image (1).png

    Step 2: Add your code#

    1. Extract demo.zip folder and open in Intellij or any preferred editor of your choice. Add the following code in DemoApplication.java.
    @SpringBootApplication public class DemoApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... args) throws Exception { System.out.println("Hello this is Demo Application"); } }

    Using CommandLineRunner in Spring Boot#

    In Spring Boot applications, the CommandLineRunner interface is a functional interface provided by Spring Boot that can be used to execute code after the Spring Boot application has started. It allows you to run specific pieces of code at startup, making it a useful tool for initialization tasks, running startup logic, or performing actions that need to happen once the application context is fully initialized.

    Overriding the run Method#

    When you override the run method, you provide the implementation for the logic that you want to execute at startup.

    Step 3: Run the application and you will see the message in your console as shown in below image#

    image (2).png

    🥳 Congratulations you have successfully created your first spring boot project.

    Now Let’s see the full directory structure of the typical spring boot application.#

    We will take an example of simple employee management testing spring boot project to understand the directory structure.#

    The below image shows well recognized folder structure for Spring boot application.#

    image (3).png
    image (4).png

    Now we will see the breakdown and significance of each folder , files in our Spring boot TestingApp.#

    Project Root Folder#

    .idea#

    This folder is created by IntelliJ IDEA and contains project-specific settings and configurations. It is not part of the Spring Boot application itself but helps in IDE configuration.

    .mvn#

    This folder contains Maven Wrapper files. The Maven Wrapper allows you to run Maven commands without needing a local Maven installation. It ensures that the project uses a specific Maven version.

    htmlReport#

    This folder is used for storing reports generated by tools like JaCoCo or other test coverage tools. It is not a standard folder but can be added for reporting purposes.

    mvnw and mvnw.cmd#

    These are the Maven Wrapper scripts for Unix/Linux and Windows, respectively. They allow you to run Maven commands using the specified Maven version without having Maven installed globally.

    pom.xml#

    This is the Maven configuration file for the project. It defines project dependencies, plugins, and other configurations required for building and managing the project.

    src Folder#

    src/main/java: Contains the main Java source code of the application.

    com.codingshuttle.TestingApp: This is the base package for your application. It contains all the application-specific classes.

    advices#

    Contains advice classes for handling cross-cutting concerns, such as GlobalExceptionHandler, which manages global exceptions.

    image (5).png

    config#

    Contains configuration classes for setting up various parts of the application, like security or custom settings.

    image (6).png

    controllers#

    Contains controller classes responsible for handling HTTP requests and responses.

    image (7).png

    dto#

    Contains Data Transfer Object classes used to transfer data between layers.

    image (8).png

    entities#

    Contains entity classes that map to database tables.

    image (9).png

    exceptions#

    Contains custom exception classes.

    image (10).png

    repositories#

    Contains repository interfaces for database operations.

    image (11).png

    services#

    Contains service classes that hold business logic.

    image (12).png

    TestingApplication.java#

    The main class that contains the main method to start the Spring Boot application.

    image (13).png

    src/main/resources: Contains configuration files and other resources. Configuration file for application settings like database connection details, server port, etc.

    application.properties#

    Configuration file for application settings like database connection details, server port, etc.

    image (14).png

    src/test Folder#

    • src/test/java: Contains Java test classes. These are used to write unit and integration tests for your application.
    • src/test/resources: Contains resources needed for tests, like test configuration files.

    target Folder#

    • classes: Contains compiled .class files for the application code.
    • generated-sources: Contains sources generated by tools during the build process.
    • generated-test-sources: Contains sources generated for test purposes.
    • test-classes: Contains compiled .class files for test code.

    Miscellaneous#

    • .gitignore: Specifies files and directories that should be ignored by Git version control.
    • Help.md: This file might be used for documentation or notes related to the project. It is not a standard file but can be useful for project-specific information.

    External Libraries and Scratches#

    • External Libraries: This section typically appears in IDEs like IntelliJ IDEA and shows the external libraries and dependencies included in the project.
    • Scratches and Consoles: This is an IDE-specific feature for temporary code snippets and interactive consoles.

    In this article, we walked through the process of creating your first Spring Boot project, starting from initializing the project using Spring Initializr to adding code and running the application with a simple "Hello" message. We explored the CommandLineRunner interface and its use in Spring Boot applications. Additionally, we examined the standard directory structure of a Spring Boot application using an employee management project as an example, explaining the significance of each folder and file. By following these steps, you now have a clear understanding of how to set up, structure, and run a basic Spring Boot project.

    Last updated on Oct 13, 2024