In the previous lesson, we saw different ways to bootstrap a Spring Boot project and we have learnt about different methods available for building and running the project. In this chapter, we will understand the different files and folders present inside the spring boot project.
Spring Boot organizes the code according to some of the best practices in the industry. A typical Spring Boot project structure is similar to what is shown below;
hello-spring ├── HELP.md ├── build ├── build.gradle ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main │ ├── java │ │ └── com │ │ └── stacktips │ │ └── hello_spring │ │ └── HelloSpringApplication.java │ └── resources │ ├── application.properties │ ├── static │ └── templates └── test └── java └── com └── stacktips └── hello_spring └── HelloSpringApplicationTests.java
The pom.xml
(Maven) or build.gradle
(Gradle)
Depending on the build tool chosen for your project, you will either have a pom.xml
file or a build.gradle
file. This file outlines the project metadata, dependencies, and build configurations essential for your project.
In this instance, we have opted for Gradle as the build tool, which is why you will find the build.gradle
file located in the root directory.
The src/main/java
Directory
The src/main/java
directory contains the Java source code files for your project. It follows the standard Java packaging structure, where the root package is called as the base package.
The src/test/java
Directory
This directory contains the unit and integration test classes for your project.
The build
Directory
The build directory is automatically generated by Maven (or Gradle) when you build the project. It contains the compiled Java classes, JAR files, and other build artifacts. You don't need to manually modify anything in this directory.
The MainApplication Class
The HelloSpringApplication
class is the entry point of your Spring Boot application. The main application class contains a main()
method, which is responsible for initializing the Spring application context and starting the Spring application.
This class is annotated with @SpringBootApplication
annotation.
The @SpringBootApplication
annotation is a convenience annotation that combines 3 other Spring Boot annotations.
- The
@EnableAutoConfiguration
enables Spring Boot's auto-configuration mechanism, which automatically configures many common Spring beans based on the dependencies that are present in the classpath. - The
@Configuration
annotation indicates that the class contains one or more bean definitions containing@Bean
annotations. - The
@ComponentScan
annotation scans the classpath for classes annotated with@Component
,@Service
,@Repository
, or@Configuration
annotations. Classes that are registered with any of the 4 annotations are registered as beans in the Spring application context.
We will learn more about this annotation in Chapter 5, Dependency Injection.
By using the @SpringBootApplication
annotation, you can simplify the configuration of your Spring Boot application as you don't need to declare the @Configuration
, @EnableAutoConfiguration
, and @ComponentScan
annotations separately.
@SpringBootApplication public class HelloSpringApplication { public static void main(String[] args) { SpringApplication.run(HelloSpringApplication.class, args); } }
The src/main/resources
directory
The resources
directory is for non-Java resources that your application needs, such as configuration files, static assets (e.g., HTML, CSS, JavaScript), templates, and other resources.
The Application Properties Class
The application.properties
file contains the configurations for your Spring Boot applications. It is located in the src/main/resources
directory. The properties file contains key-value pairs separated by equals =
, where the key is the name of the property and the value is the value of the property.
Static Content
The resources/static
folder is dedicated to all the static resources, templates, and property files. This folder contains static resources such as CSS, JS, and images.
resources/templates
The resources/templates
directory is the default location for storing server-side view templates that render dynamic content on web pages. These templates are processed by a templating engine to produce the final HTML output, which is then sent as a response to the browser.
Spring Boot natively supports the following template engines:
- Thymeleaf: This is the default template engine in Spring Boot, activated through the
spring-boot-starter-thymeleaf
starter dependency. - FreeMarker: Another popular option, FreeMarker can be enabled using the
spring-boot-starter-freemarker
starter dependency. - Mustache: A logic-less templating engine that emphasizes simplicity and separation of concerns, activated via the
spring-boot-starter-mustache
dependency.
All of these template engines automatically resolve template files from the /resources/templates directory.