SoFunction
Updated on 2025-04-13

Detailed explanation of the usage of configuration files in SpringBoot

Spring Boot'sFiles are the core configuration files of the Maven project, which are used to define project dependencies, plug-ins, build configuration and other information. The following is a Spring Boot projectDetailed analysis of the file:

1. Basic structure

The basic structure of the file is as follows:

<project xmlns="/POM/4.0.0"
         xmlns:xsi="http:///2001/XMLSchema-instance"
         xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
    <modelVersion>4.0.0</modelVersion>

    <!-- Basic project information -->
    <groupId></groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <!-- Project name and description -->
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <!-- Parent project -->
    <parent>
        <groupId></groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!-- Project dependency -->
    <dependencies>
        <!-- Spring Boot Starter rely -->
        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 其他rely -->
    </dependencies>

    <!-- Build configuration -->
    <build>
        <plugins>
            <!-- Spring Boot Maven Plugin -->
            <plugin>
                <groupId></groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2. Detailed explanation of key parts

2.1 <modelVersion>

Specifies the version of the POM model, Maven 2 and Maven 3 use 4.0.0.

<modelVersion>4.0.0</modelVersion>

2.2 Project coordinates

  • <groupId>: A unique identifier for an organization or project (for example:).
  • <artifactId>: The unique identifier of the project (for example: demo).
  • <version>: The version number of the project (for example: 0.0.1-SNAPSHOT).
  • <packaging>: How to package projects, Spring Boot usually uses jars.
<groupId></groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

2.3 <parent>

Spring Boot projects are usually inherited from spring-boot-starter-parent, which provides default dependency management, plug-in configuration, and resource filtering.

&lt;parent&gt;
    &lt;groupId&gt;&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    &lt;version&gt;3.1.0&lt;/version&gt;
    &lt;relativePath/&gt; &lt;!-- Find parent project from the warehouse --&gt;
&lt;/parent&gt;

2.4 <dependencies>

Define the dependencies required by the project. Spring Boot provides many starter dependencies for simplifying dependency management.

Spring Boot Starter dependencies:

  • spring-boot-starter-web: used to build web applications.
  • spring-boot-starter-data-jpa: for JPA and data access.
  • spring-boot-starter-test: for unit testing.
<dependencies>
    <dependency>
        <groupId></groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId></groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Other dependencies:

If you need additional libraries (such as database drivers, tool libraries, etc.), you can add them here.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.26</version>
</dependency>

2.5 <build>

Defines the build configuration of the project, including plug-ins and resource filtering.

Spring Boot Maven Plugin:

Used to package executable JAR files and support the running of Spring Boot applications.

<build>
    <plugins>
        <plugin>
            <groupId></groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Resource Filtering:

If you need to replace placeholders in the configuration file (such as ${}), you can enable resource filtering.

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

2.6 <properties>

Define project properties to be used to uniformly manage version numbers or other configurations.

<properties>
    <>17</>
    <>3.1.0</>
</properties>

2.7 <dependencyManagement>

Used to centrally manage dependent version numbers, usually used with BOM (Bill of Materials).

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2.8 <profiles>

Defines a Maven-built Profile for using different configurations in different environments.

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <env>dev</env>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <env>prod</env>
        </properties>
    </profile>
</profiles>

3. Common configuration examples

3.1 Multi-module project

If the project is multi-module, submodules can be defined in the parent project.

<modules>
    <module>module1</module>
    <module>module2</module>
</modules>

3.2 Custom packaging

If you need to customize the packaging method, you can configure maven-assembly-plugin.

<plugin>
    <groupId></groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptor>src/assembly/</descriptor>
    </configuration>
</plugin>

3.3 Resource Filtering

Replace the placeholder in the configuration file.

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

4. Summary

Spring Boot files are the core configuration files of Maven projects, through which you can manage dependencies, plug-ins, build configurations, etc. Key points include:

  • Inherit spring-boot-starter-parent to simplify configuration.
  • Use spring-boot-starter-* to quickly introduce functional modules.
  • Configure spring-boot-maven-plugin to support the packaging and running of Spring Boot applications.
  • Use <properties> and <dependencyManagement> to manage version numbers uniformly.

Through reasonable configuration, the development efficiency and maintainability of Spring Boot projects can be greatly improved.

This is the end of this article about the detailed explanation of the use of configuration files in SpringBoot. For more related SpringBoot content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!