Spring Boot is a popular open-source framework for building Java applications. It simplifies the development process by providing a default project structure that includes all necessary components and configurations. In this tutorial, we will explore the default project structure of a Spring Boot application and understand its key components.
A typical Spring Boot project follows a standard directory layout that helps developers organize their code efficiently. Understanding this structure is crucial for anyone looking to develop or maintain a Spring Boot application. The default project structure includes several directories and files, each serving a specific purpose.
Let's take a look at a typical Spring Boot project structure:
my-spring-boot-app
āāā src
ā āāā main
ā ā āāā java
ā ā ā āāā com
ā ā ā āāā example
ā ā ā āāā demospringboot
ā ā ā āāā DemoSpringBootApplication.java
ā ā ā āāā controller
ā ā ā āāā HelloController.java
ā ā āāā resources
ā ā āāā application.properties
ā ā āāā static
ā āāā test
ā āāā java
ā āāā com
ā āāā example
ā āāā demospringboot
ā āāā DemoSpringBootApplicationTests.java
āāā pom.xml
āāā README.md
main method. It serves as the entry point for the Spring Boot application.1package com.example.demospringboot;23import org.springframework.boot.SpringApplication;4import org.springframework.boot.autoconfigure.SpringBootApplication;56@SpringBootApplication7public class DemoSpringBootApplication {89public static void main(String[] args) {10SpringApplication.run(DemoSpringBootApplication.class, args);11}12}
1package com.example.demospringboot.controller;23import org.springframework.web.bind.annotation.GetMapping;4import org.springframework.web.bind.annotation.RestController;56@RestController7public class HelloController {89@GetMapping("/hello")10public String sayHello() {11return "Hello, World!";12}13}
1server.port=80802spring.application.name=demo-spring-boot-app
1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"2xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">3<modelVersion>4.0.0</modelVersion>4<parent>5<groupId>org.springframework.boot</groupId>6<artifactId>spring-boot-starter-parent</artifactId>7<version>3.0.0</version>8<relativePath/> <!-- lookup parent from repository -->9</parent>10<groupId>com.example</groupId>11<artifactId>demospringboot</artifactId>12<version>0.0.1-SNAPSHOT</version>13<name>DemoSpringBoot</name>14<description>Demo project for Spring Boot</description>15<properties>16<java.version>17</java.version>17</properties>18<dependencies>19<dependency>20<groupId>org.springframework.boot</groupId>21<artifactId>spring-boot-starter-web</artifactId>22</dependency>2324<dependency>25<groupId>org.springframework.boot</groupId>26<artifactId>spring-boot-starter-test</artifactId>27<scope>test</scope>28</dependency>29</dependencies>3031<build>32<plugins>33<plugin>34<groupId>org.springframework.boot</groupId>35<artifactId>spring-boot-maven-plugin</artifactId>36</plugin>37</plugins>38</build>39</project>
Let's walk through a simple example to demonstrate how to create and run a Spring Boot application.
Create a new Spring Boot project using Spring Initializr (https://start.spring.io/). Choose Maven as the build tool, Java as the language, and add dependencies like "Spring Web".
Download the generated project and extract it to your local machine.
Open the project in your favorite IDE (e.g., IntelliJ IDEA or Eclipse).
Run the application by executing the main method in DemoSpringBootApplication.java.
Access the application by navigating to http://localhost:8080/hello in your web browser. You should see the output "Hello, World!".
In the next section, we will explore how to manage dependencies using Maven or Gradle. Understanding dependency management is essential for building robust and scalable applications.