Welcome back! In the previous lesson, you got introduced to Spring Boot and saw how it simplifies Java application development. Now, let's dive into understanding the project structure of a Spring Boot application. Grasping the project structure is crucial as it helps you know where to place your code and configuration files. By the end of this lesson, you'll have a clear understanding of the typical Spring Boot project layout and the purpose of specific files. You'll also cover some basic Gradle commands to help you manage your Spring Boot application efficiently.
A typical Spring Boot project follows a standardized structure, especially when using Gradle as the build tool. Adhering to this structure ensures compatibility and ease of integration. Here's an outline of your project:
1spring-boot-intro/ 2├── src/ 3│ ├── main/ 4│ │ ├── java/ 5│ │ │ └── com/ 6│ │ │ └── codesignal/ 7│ │ │ └── MyApplication.java 8│ │ ├── resources/ 9│ │ └── application.properties 10│ ├── test/ 11│ │ ├── java/ 12│ │ │ └── com/ 13│ │ │ └── codesignal/ 14│ │ │ └── MyApplicationTests.java 15├── build.gradle
Let's break down the key directories and files:
src/main/java
: This directory contains your main source code. This is where you write your Java classes and define the core logic of your application.src/main/resources
: Holds resources like configuration files (application.properties
), static files, and templates. Resources in this directory are included in the classpath.src/test/java
: This directory contains your test cases. It mirrors the structure ofsrc/main/java
but is specifically for testing your application.
These directories form the backbone of your project, ensuring it is organized and modular.
Let's take a closer look at some of the essential files in your Spring Boot project:
MyApplication.java
: This is the entry point of the Spring Boot application.build.gradle
: The Gradle build specification file, which contains dependencies and plugins necessary for building the project.application.properties
: Located insrc/main/resources
, this file is used for configuring various aspects of the application.MyApplicationTests.java
: Located insrc/test/java
, this simple test class ensures that the Spring application context loads successfully. You'll add more tests as you develop the application.
The build.gradle
file is the script used by Gradle to build the project. It is written using Groovy scripting language, but it can also be authored using Kotlin DSL. This file specifies the project dependencies, plugins, and other configurations. Here's an example build.gradle
file for a Spring Boot project:
Groovy1plugins { 2 id 'org.springframework.boot' version '2.5.4' 3 id 'io.spring.dependency-management' version '1.0.11.RELEASE' 4 id 'java' 5} 6 7group = 'com.codesignal' 8version = '0.0.1-SNAPSHOT' 9sourceCompatibility = '11' 10 11repositories { 12 mavenCentral() 13} 14 15dependencies { 16 implementation 'org.springframework.boot:spring-boot-starter-web' 17 testImplementation 'org.springframework.boot:spring-boot-starter-test' 18} 19 20test { 21 useJUnitPlatform() 22}
Key components:
- Plugins: Used to add functionality to the build script. The
org.springframework.boot
plugin is particularly important because it allows you to use Spring Boot without having to specify the specific versions for spring dependencies. This ensures that your dependencies are compatible and simplifies the management of library versions. It also provides thegradle bootRun
command that lets you run your Spring Boot application from the command line. - Dependencies: These specify the libraries required for the project. Notice that the dependencies are mostly "starter" dependencies like
spring-boot-starter-web
. These starter dependencies are special in that they typically don’t have any library code themselves but instead transitively pull other libraries. This keeps your build file significantly smaller and easier to manage. You can think of your dependencies in terms of capabilities rather than individual library names. - Repositories: Locations where Gradle will look for these dependencies.
mavenCentral()
is a widely used repository that hosts a large number of Java libraries. - Group and Version: These define the naming for the resulting artifact (e.g., JAR file).
group
sets the base package name, andversion
specifies the version of the artifact. - Source Compatibility:
sourceCompatibility = '11'
sets the Java version compatibility to Java 11. This ensures that the Java compiler uses Java 11 features and syntax. - Test Configuration:
test { useJUnitPlatform() }
specifies that JUnit Platform should be used for running the tests. This enables you to leverage modern JUnit 5 features. Don't worry about the tests for now; we'll cover them in detail later in the course path.
Following Gradle conventions ensures your project is well-structured and manageable.
The cornerstone of a Spring Boot application is its bootstrap class. In your case, it's named MyApplication.java
. This class contains the main
method that Spring Boot uses to start the application. Let's break down the bootstrap class:
Java1@SpringBootApplication 2public class MyApplication { 3 4 public static void main(String[] args) { 5 SpringApplication.run(MyApplication.class, args); 6 } 7}
The @SpringBootApplication
annotation is particularly powerful. You'll delve into it more in later lessons, but for now, understand that this annotation signals Spring Boot to auto-configure the application based on the dependencies and configurations present. This is one of many annotations that Spring relies on heavily to simplify configurations and enhance functionality.
The other crucial component is the main()
method. This method serves as the entry point of the application and is executed when the JAR file is run. The line SpringApplication.run(MyApplication.class, args);
tells Spring Boot to start the application, create the Spring application context, and start the embedded Tomcat server. In most cases, this method will remain boilerplate code across different Spring Boot applications.
Understanding the bootstrap class is essential, as it sets up and runs your Spring Boot application.
Now that you're familiar with the project structure and key files, it's important to learn how to manage the application using Gradle. The Gradle CLI provides several commands for building, testing, and managing the project. Here are some of the most commonly used commands:
gradle build
: Compiles and assembles the project. This command includes running tests as part of the build process.gradle clean
: Deletes the build directory to ensure a clean slate for the next build.gradle test
: Runs the tests.gradle bootRun
: Runs the Spring Boot application.
Ensure you execute these commands from the root directory of your Gradle project, where the build.gradle
file is located.
If you're new to web development, you might be accustomed to writing programs that run for a finite duration and then terminate. However, when building a web application, the application needs to run continuously to receive HTTP requests from users and respond to them. Given that Spring Boot is predominantly used for web development, it incorporates an embedded Tomcat server. This server runs your application indefinitely, listening for incoming web requests. By default, the application runs on port 8080. As a result, when you execute the gradle bootRun
command, your application doesn't simply run and exit; it starts up and remains active until you manually stop it.
To wrap up, you explored the standard structure of a Spring Boot project, emphasizing Gradle conventions, and examined the crucial directories and files, including the bootstrap class MyApplication.java
, build.gradle
, and application.properties
. You discussed the role of the build.gradle
file and its components, along with the purpose and annotations of the bootstrap class. You also got familiar with some essential Gradle commands that enable you to run the Spring Boot application or execute tests. This foundational knowledge prepares you to dive deeper into building a Spring Boot application. Happy coding!