Detailed explanation of classpath in SpringBoot
Understand when developing Spring Boot applicationsclasspath
The concept of configuring, resource management, and building projects is very important. Especially when we use the Maven packaging tool, the project's resource files will be placed in different directories at different stages.
This article will explore in-depth about Spring Bootclasspath
and how related resources are managed, especially when we use Maven to build, how resource files are from the development environmentresources
Move the directory to the final JAR packageBOOT-INF/classes
Table of contents.
1. What is classpath?
classpath
Is the path used by Java programs to find classes and resource files. It specifies the path to which the JVM loads classes and resources at runtime.
Spring Boot applications, especially when built with Maven,classpath
Plays a crucial role because it includes not only Java class files, but also other resource files, such as configuration files, static files, template files, etc.
2. Resource Management in Spring Boot
In Spring Boot, all resource files, such as、
, static files (e.g.
.html
, .js
, .css
) and template files (e.g..ftl
、.thymeleaf
etc.) can be placed insrc/main/resources
In the directory.
2.1 Storage of resource files in the development stage
During the development stage, Spring Boot uses Maven build tools, and the resource files in the project are generally stored insrc/main/resources
In the directory.
src/main/resources
Directory structure:
src └── main └── resources ├── ├── static │ ├── js │ └── css ├── templates │ └── └── other-files └──
During this stage, Spring Boot will load directlysrc/main/resources
Files in the directory, no additional configuration is required.
Spring Boot Automatic Scanresources
Files in the directory and use them asclasspath
The resources in it are loaded.
2.2 Compilation stage
When we build a project using Maven, the resource file is copied totarget/classes
In the directory.
Mavenresources
Plugins to process these resources and ensure that all resource files are transferred fromsrc/main/resources
Copy totarget/classes
Table of contents.
This directory is what we call the classpath (classpath
part of ).
target/classes
Directory structure:
target └── classes ├── ├── static │ ├── js │ └── css ├── templates │ └── └── other-files └──
At this time, Spring Boot's application can already be directly fromtarget/classes
The directory loads the resource file.
3. Package as a JAR file
When building a Spring Boot project using Maven package, the final output is a JAR file containing all dependencies and resources.
Spring Boot uses the Maven plugin to create an executable JAR file that contains all the project's.class
Files and resource files.
3.1 Directory structure in JAR file
The JAR file created by Spring Boot is a self-contained file that contains all the necessary components of the application, and the resource file will be placed inBOOT-INF/classes
In the directory.
This is different from traditional JAR files, which place resource files directly in the root directory, while Spring Boot will place all resource files inBOOT-INF/classes
and maintain the original directory structure.
target
JAR file structure in directory:
target └── ├── BOOT-INF │ └── classes │ ├── │ ├── static │ │ ├── js │ │ └── css │ ├── templates │ │ └── │ └── other-files │ └── ├── META-INF └── org └── springframework └── boot └── loader
In this structure, Spring Boot's resource files are organized inBOOT-INF/classes
In the directory. All resource files, including, static resources and template files are all located in this directory.
Maven will transfer these files fromtarget/classes
Copy the directory to the JAR packageBOOT-INF/classes
Table of contents.
3.2 BOOT-INF/classes directory
The internal structure of Spring Boot's JAR package includes:
-
BOOT-INF/classes
: This is the directory where the application's class and resource files are located. Spring Boot will load all classes and resources from this directory. -
BOOT-INF/lib
: This directory contains all the application's dependencies JAR files, which Spring Boot will load as application's dependencies. -
META-INF
: This directory contains metadata of JAR files, such as MANIFEST files, Spring Boot launcher configuration, etc.
4. Maven configuration
Maven usesspring-boot-maven-plugin
Plugin to create executable JAR files.
This plug-in automatically handles the generation process of JAR file and correctly places the application's class and resource files in the corresponding location of the JAR file.
Example:
<dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Other dependencies --> </dependencies> <build> <plugins> <!-- Spring Boot Maven Plugin --> <plugin> <groupId></groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
In Maven,spring-boot-maven-plugin
Copy of resource files will be processed automatically and placed correctly inBOOT-INF/classes
In the directory. It ensures that all necessary files are included in the JAR file so that the application can run correctly.
5. Run the JAR file
When we executejava -jar
Spring Boot will use it when starting a Spring Boot applicationBOOT-INF/classes
Resource files and classes in the directory to start the application.
Spring Boot startup process:
- Spring Boot will automatically recognize the JAR package
BOOT-INF/classes
directory and load it as classpath. - Configuration files, static resources and template files are all from
BOOT-INF/classes
load in to ensure that the application's resources are loaded correctly.
6. Resource loading mechanism
Spring Boot loads resources in the JAR file through the class loader at startup.
If you need to access resources, Spring Boot will depend on Spring'sResourceLoader
To provide the rightclasspath
access support for resources in.
@Autowired private ResourceLoader resourceLoader; public void loadFile() throws IOException { Resource resource = ("classpath:"); InputStream inputStream = (); // Read file content}
ResourceLoader
passclasspath:
Prefix to access resources under the classpath.
Summarize
In Spring Boot,classpath
It is a key part of the storage and access of resource files in applications. During the development stage, resource files are stored insrc/main/resources
In the directory, it is copied totarget/classes
in the directory.
When finally packaged as a JAR file, the resource file will be placed inBOOT-INF/classes
In the directory, this is different from the structure of traditional JAR files.
When Spring Boot is built using Maven,spring-boot-maven-plugin
Copying and organization of resource files is automatically processed to ensure that they are loaded correctly when the application starts.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.