Spring Boot Project Structure
By default, The Spring Boot Project structure looks as follows:
It is a default directory created by Spring Boot using the Spring Initializr. We have not included any file or data in it. It contains the following file data:
SpringBootDemoApplication.java:
package com.javasterling.sb.SpringBootDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.javasterling.sb</groupId>
<artifactId>SpringBootDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties file is empty.
SpringBootDemoApplicationTests.java:
package com.javasterling.sb.SpringBootDemo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringBootDemoApplicationTests {
@Test
void contextLoads() {
}
}
JRE System Library
JRE System Library contains the following library files:
Maven Dependencies
It contains lots of dependencies. Later we will discuss them all in the other section. It contains the following dependencies:
Output:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.1)
2020-12-20 20:48:14.307 INFO 23242 --- [ main] o.s.boot.SpringApplication : Starting SpringApplication v2.4.1 using Java 1.8.0_275 on alien-Inspiron-3542 with PID 23242 (/home/alien/.m2/repository/org/springframework/boot/spring-boot/2.4.1/spring-boot-2.4.1.jar started by alien in /home/alien/Documents/SpringBootDemo)
2020-12-20 20:48:14.421 INFO 23242 --- [ main] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default
2020-12-20 20:48:14.558 ERROR 23242 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalArgumentException: Sources must not be empty
at org.springframework.util.Assert.notEmpty(Assert.java:470) ~[spring-core-5.3.2.jar:5.3.2]
at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:412) [spring-boot-2.4.1.jar:2.4.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:325) [spring-boot-2.4.1.jar:2.4.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) [spring-boot-2.4.1.jar:2.4.1]
at org.springframework.boot.SpringApplication.main(SpringApplication.java:1325) [spring-boot-2.4.1.jar:2.4.1]
Consideration while Maintaining the Spring Boot Project
The Spring Boot provides high customization and flexibility to organize classes. But, there are still some recommendations to organize the Spring Boot project structure. Consider the following points to organize your Spring Boot Project:
Avoid the use of the Default Package
It is recommended to avoid the use of the default packages. Since, The Spring Boot annotations such as @ComponentScan
, @EntityScan
, @ConfigurationPropertiesScan
and @SpringBootApplication
use packages to define scanning locations. So, we should always declare the packages in our classes.
Main Class should be in Base Package.
The main class should be in the base package, as the @SpringBootApplication
annotation triggers component scanning for the current package and its sub-packages. However, the main class is configurable; it can be placed and located elsewhere by specifying the base package manually. But, defining it to the base package must be simpler.
If our project is a JPA-based project, then we have to perform some additional configurations because a JPA-based project will have few more annotations on the main class. It has the following extra annotations:
@SpringBootApplication(scanBasePackages = "example.javasterling.com")
@EnableJpaRepositories("example.javasterling.com")
@EntityScan("example.javasterling.com")
The design must be Imposed as per requirement
The Spring Boot project is independent of design. Therefore, it should be maintained as per requirement.
The developers prefer the package-by-feature design strategy, which enhances the project’s modularity. For example, a library management project should be organized packaged by features strategy. Hence we will have the main package as org.springframework.samples.libraryManagement
, and it’s sub-packages will be:
org.springframework.samples.libraryManagement.bookName
org.springframework.samples.libraryManagement.writer
org.springframework.samples.libraryManagement.price
org.springframework.samples.libraryManagement.availability
Each of the above packages represents a specific domain, or it’s a feature of the project.