SpringBoot学习笔记(一)——通过Maven搭建SpringBoot项目

概述:Spring Boot可以快速构建可运行的独立的,生产级的基于Spring的应用程序

1.Spring Boot使用的系统要求:

2.通过Maven构建Spring Boot项目

注:Spring Boot与Apache Maven3.3或以上版本兼容

典型的POM如下:

<?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     
         http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.com.springboot.demo</groupId>
    <artifactId>springboot-1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot  -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

此时依赖关系如图:

通过继承spring-boot-starter-parent的方式很方便,但是有的时候真实项目场景中,我们可能需要继承其他的必要的父POM,或者是我们不希望使用Spring Boot的一些默认Maven设置,在这些情况下,可以通过使用scope=import依赖关系来保持依赖关系管理。

POM如下:

<?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 
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.com.springboot.demo</groupId>
    <artifactId>springboot-1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- 从Spring Boot导入依赖关系管理 -->
                <groupId> org.springframework.boot </groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <!-- Package as an executable jar/war -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

此时依赖关系与上面的依赖图一

另外,官网上描述,通过以下依赖配置,我们可以指定版本,例如Spring Data的版本,如果我们不额外配置spring-data-releasetrain版本,会使用spring-boot-dependencies中指定的版本,其他依赖同理

<!--覆盖Spring Boot提供的Spring Data发布系列 -->
<dependency>
    <groupId> org.springframework.data </groupId>
    <artifactId> spring-data-releasetrain</artifactId>
    <version>Fowler-SR2 </version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

引入所需的jar包后需要创建主启动类(根包下),默认以8080端口启动

猜你喜欢

转载自blog.csdn.net/Peacock__/article/details/86011359