Spring Boot is a Java-based framework that helps developers build applications easily without complex configurations. This course will cover how to structure the main
directory of a Spring Boot project. This directory serves as the starting point for a Java application and is where the important business logic is implemented.
Spring Boot Project Structure
A Spring Boot project follows a predefined structure. When you create a Spring Boot project in your IDE, a basic structure like the one below is generated.
src ├── main │ ├── java │ │ └── com │ │ └── example │ │ └── demo │ │ ├── DemoApplication.java │ │ └── controller │ │ └── service │ │ └── repository │ └── resources │ ├── application.properties │ └── static │ └── templates └── test └── java
1. src/main/java Directory
The src/main/java
directory is where the actual Java source code is located, and each package and class file is stored here. In Spring Boot, packages are generally structured in the format of com.example.demo
.
1.1 Main Application Class
The DemoApplication.java
file is the entry point of the Spring Boot application. This class is annotated with @SpringBootApplication
, which encompasses the following three functionalities:
@Configuration
: A Java-based configuration class.@EnableAutoConfiguration
: Enables Spring Boot’s auto-configuration feature.@ComponentScan
: Automatically scans the specified packages to discover Spring components.
Below is an example of the main application class.
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
1.2 Package Structure
When there are many source files, defining an appropriate package structure is important. Typically, packages such as controller
, service
, and repository
are defined.
Controller Package
The controller
package contains methods that handle requests and return responses. In a RESTful API, the @RestController
annotation is mainly used to set up the REST API server.
package com.example.demo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, World!"; } }
Service Package
The service
package includes classes that handle business logic. The classes here are registered in the Spring context using the @Service
annotation.
package com.example.demo.service; import org.springframework.stereotype.Service; @Service public class HelloService { public String getGreeting() { return "Hello from Service!"; } }
Repository Package
The repository
package is responsible for interacting with the database. It usually extends JpaRepository
to provide CRUD functionality.
package com.example.demo.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.example.demo.model.User; @Repository public interface UserRepository extends JpaRepository{ }
2. src/main/resources Directory
The src/main/resources
directory is where static files, templates, and configuration files used by the application are located. The main files and directories in this location are as follows.
2.1 application.properties
The configuration file application.properties
is responsible for application environment settings. Here, you can set database configurations, port numbers, log levels, etc.
spring.datasource.url=jdbc:mysql://localhost:3306/demo spring.datasource.username=root spring.datasource.password=password server.port=8080 logging.level.org.springframework=DEBUG
2.2 static Directory
The static
directory is where static resources like CSS, JavaScript, and image files are stored. Spring Boot automatically serves all files in this directory.
2.3 templates Directory
The templates
directory is used to dynamically generate HTML files using a template engine like Thymeleaf. You can create HTML files here and inject dynamic data.
Welcome Welcome Message
3. src/test/java Directory
The src/test/java
directory contains sources related to the application’s tests. Unit tests and integration tests are performed using testing frameworks like JUnit and Mockito.
package com.example.demo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class DemoApplicationTests { @Autowired private HelloService helloService; @Test void contextLoads() { assertNotNull(helloService); } }
Conclusion
In this course, we have explored in detail how to structure the main
directory of a Spring Boot project. By understanding the project structure and configuring it correctly, developers can write more efficient and maintainable code. This structure will significantly aid in future maintenance and collaboration. Wishing you successful Spring Boot backend development.