SpringBoot2.x入门:快速创建一个SpringBoot应用

前提

这篇文章是《SpringBoot2.x入门》专辑的第2篇文章,使用的SpringBoot版本为2.3.1.RELEASEJDK版本为1.8

常规的套路会建议使用Spring官方提供的工具Spring Initializr通过指定配置创建一个SpringBoot项目,但是考虑到Spring Initializr必须联网使用,对于项目配置和依赖的控制粒度不够精细,本文会从更一般的情况考虑,详细分析怎么通过MavenIntelliJ IDEA(下称IDEA)快速创建一个SpringBoot应用,包括单模块的Maven和多模块的Maven应用创建。

依赖分析

必要的插件:

  • Maven编译插件:maven-compiler-plugin
  • SpringBoot封装的Maven插件(一般必选,项目最终打包依赖到这个插件,它的版本建议跟随选用的SpringBoot的版本):spring-boot-maven-plugin

Maven编译插件尽可能选用高版本,以适配更高版本的JDK。一般会选用的基本依赖如下:

  • lombok(可选,个人认为能提高开发效率,不过需要安装对应的插件)。
  • junitspring-boot-starter-test):单元测试。
  • spring-boot-starterBean管理、配置读取等,简单理解就是IOC容器核心组件和一些扩展。
  • spring-boot-starter-web:基于spring-boot-starter扩展,主要集成了SpringMVC的功能。

多数情况下,选用spring-boot-starter-web即可,版本选取REALEASE版本即可,注意尽可能整套项目使用同一个大版本的SpringBoot。

下面例子用到的各个组件的版本如下:

序号 组件 版本号 描述
1 maven-compiler-plugin 3.8.1 Maven编译插件
2 spring-boot-starter 2.3.1.RELEASE IOC容器核心组件
3 spring-boot-maven-plugin 2.3.1.RELEASE SpringBoot封装的Maven插件
4 lombok 1.18.12 -

创建一个单模块的SpringBoot应用

点击IDEA主菜单File -> Project进入创建新项目的界面:

SpringBoot2.x入门:快速创建一个SpringBoot应用_第1张图片

选择左侧的Maven选项,上方下拉选择好JDK版本,勾选Create from archetype,然后选中maven-archetype-webapp这个骨架的RELEASE版本,然后点击下一步按钮:

SpringBoot2.x入门:快速创建一个SpringBoot应用_第2张图片

输入项目的GAV,选定项目的磁盘目录,然后点击下一步按钮:

SpringBoot2.x入门:快速创建一个SpringBoot应用_第3张图片

选定Maven的安装路径、配置文件和本地仓库,配置好相应的属性,最后点击完成即可:

SpringBoot2.x入门:快速创建一个SpringBoot应用_第4张图片

创建出来的是一个标准的Maven项目,它的结构如下:

spring-boot-guide
   - src
     - main
       - webapp
         - web.xml
   - pom.xml

一般可以直接删除src/main/webapp目录,在pom.xml中增加对应的依赖,最终的pom.xml如下:



    4.0.0
    club.throwable
    spring-boot-guide
    1.0-SNAPSHOT
    
    jar
    spring-boot-guide
    
        UTF-8
        1.8
        1.8
        3.8.1
        1.18.12
        2.3.1.RELEASE
    
    
    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring.boot.version}
                import
                pom
            
        
    
    
        
            org.projectlombok
            lombok
            ${lombok.version}
            provided
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
        spring-boot-guide
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                ${maven.compiler.plugin.version}
                
                    ${maven.compiler.source}
                    ${maven.compiler.target
                    ${project.build.sourceEncoding}
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${spring.boot.version}



                
                    
                        
                            repackage
                        
                    
                
            
        
    

有依赖版本变动,只需要直接修改properties元素中对应的版本号即可。在src/main下新建启动类java/club/throwable/App.java

SpringBoot2.x入门:快速创建一个SpringBoot应用_第5张图片

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@Slf4j
@SpringBootApplication
public class App implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("Hello SpringBoot!");
    }
}

启动App中的main函数后输出如下:

SpringBoot2.x入门:快速创建一个SpringBoot应用_第6张图片

spring-boot-starter模块引入的只是核心容器组件,并没有集成像Tomcat这样的Servlet容器,启动后不会挂起主线程,所以执行完CommandLineRunner中的逻辑就会自行退出主线程。

创建一个多模块的SpringBoot应用

多模块应用的创建基于单模块应用,准确来说就是在一个创建完的单模块应用中添加新的模块(New Module)。在原来的根项目spring-boot-guide右键弹出菜单中选择新建模块:

SpringBoot2.x入门:快速创建一个SpringBoot应用_第7张图片

后续的步骤与上一小节的过程完全相同,不过定义的模块名称必须和根项目的名称不相同,这里定义为ch0-dependency,然后调整父pom.xml和子pom.xml

  • spring-boot-guide -> pom.xml


    4.0.0
    club.throwable
    spring-boot-guide
    1.0-SNAPSHOT
    
        ch0-dependency
    
    pom
    spring-boot-guide
    
        UTF-8
        1.8
        1.8
        3.8.1
        1.18.12
        2.3.1.RELEASE
    
    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring.boot.version}
                import
                pom
            
        
    
    
        
            org.projectlombok
            lombok
            ${lombok.version}
            provided
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        spring-boot-guide
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                ${maven.compiler.plugin.version}
                
                    ${maven.compiler.source}
                    ${maven.compiler.target
                    ${project.build.sourceEncoding}
                
            
        
    

  • spring-boot-guide/ch0-dependency -> pom.xml


    4.0.0
    
        club.throwable
        spring-boot-guide
        1.0-SNAPSHOT
    
    ch0-dependency
    1.0-SNAPSHOT
    jar
    ch0-dependency
    
        
            org.springframework.boot
            spring-boot-starter
        
    
    
        ch0-dependency
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${spring.boot.version}


                           
                
                    
                        
                            repackage
                        
                    
                
            
        
    

注意:

  • spring-boot-maven-plugin一般情况下只需配置在需要打包的模块中,一般父模块是全局管理的模块,不需要全局定义此插件。
  • maven-compiler-plugin可以配置在父模块中,让所有子模块都应用此插件。
  • spring-boot-starter-testlombok可以在父模块的dependencies元素中添加,相当于所有子模块都引入了这两个依赖。

父模块中的spring-boot-guidesrc模块需要丢弃,可以直接剪切到ch0-dependency子模块中,如下:

SpringBoot2.x入门:快速创建一个SpringBoot应用_第8张图片

后面再添加其他新的模块,直接重复上述的步骤即可。

代码仓库

这里给出本文搭建的一个多模块的SpringBoot应用的仓库地址(持续更新):

  • Github:https://github.com/zjcscut/spring-boot-guide

(本文完 c-2-d e-a-20200701 8:39 AM)

技术公众号(《Throwable文摘》,id:throwable-doge),不定期推送笔者原创技术文章(绝不抄袭或者转载):

你可能感兴趣的:(SpringBoot2.x入门:快速创建一个SpringBoot应用)