Spring Boot是在Spring框架上创建的一个全新的框架,其设计目的是简化Spring应用的搭建和开发过程。开启Spring Boot有许多种方法可供选择,这里仅介绍使用http://start.spring.io/来构建一个简单的Spring Boot项目。
生成项目文件
访问http://start.spring.io/,页面显示如下:

这里选择以Maven构建,语言选择Java,Spring Boot版本为2.3.2。
在项目信息里选择以jar包的方式部署,Java版本为8。
最后点击页面的
按钮生成项目文件。文件下载后是一个压缩包,进行解压然后使用IDEA打开。项目目录如下所示:

简单演示
项目根目录下生成了一个artifactId+Application命名规则的入口类,为了演示简单,不再新建控制器,直接在入口类中编写代码:

然后在IDEA中点击运行启动入口:
![]()
访问http://localhost:8080,页面显示如下:

打包发布
在IDEA中右击项目,选择Maven模块,勾选跳过测试按钮,如下图所示:

然后双击package就将项目打包成jar包(初次打包会自动下载一些依赖)。

看到BUILD SUCCESS即表示打包完成。
打包完毕后可看到项目目录target文件夹下生成了一个jar文件:

生成jar包后,cd到target目录下,执行以下命令:

访问http://localhost:8080,效果如上。
聊聊pom.xml
打开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.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring-boot-starter-parent
spring-boot-starter-parent指定了当前项目为一个Spring Boot项目,它提供了诸多的默认Maven依赖,具体可查看目录C:\.m2\repository\org\springframework\boot\spring-boot-dependencies\2.3.2.RELEASE下的spring-boot-dependencies-2.3.2.RELEASE.pom文件,这里仅截取一小部分:
<properties>
...
<spring-amqp.version>2.2.9.RELEASE</spring-amqp.version>
<spring-batch.version>4.2.4.RELEASE</spring-batch.version>
<spring-data-releasetrain.version>Neumann-SR2</spring-data-releasetrain.version>
<spring-framework.version>5.2.8.RELEASE</spring-framework.version>
<spring-hateoas.version>1.1.0.RELEASE</spring-hateoas.version>
<spring-integration.version>5.3.2.RELEASE</spring-integration.version>
<spring-kafka.version>2.5.4.RELEASE</spring-kafka.version>
<spring-ldap.version>2.3.3.RELEASE</spring-ldap.version>
<spring-restdocs.version>2.0.4.RELEASE</spring-restdocs.version>
<spring-retry.version>1.2.5.RELEASE</spring-retry.version>
<spring-security.version>5.3.3.RELEASE</spring-security.version>
<spring-session-bom.version>Dragonfruit-RELEASE</spring-session-bom.version>
<spring-ws.version>3.0.9.RELEASE</spring-ws.version>
<sqlite-jdbc.version>3.31.1</sqlite-jdbc.version>
<sun-mail.version>1.6.5</sun-mail.version>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-extras-data-attribute.version>2.0.1</thymeleaf-extras-data-attribute.version>
<thymeleaf-extras-java8time.version>3.0.4.RELEASE</thymeleaf-extras-java8time.version>
<thymeleaf-extras-springsecurity.version>3.0.4.RELEASE</thymeleaf-extras-springsecurity.version>
<thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version>
<tomcat.version>9.0.37</tomcat.version>
...
</properties>
当然,我们可以手动改变这些依赖的版本。比如我想把kafka的版本改为2.5.5.RELEASE,我们可以在pom.xml中进行如下配置:
<properties>
<spring-kafka.version>2.5.5.RELEASE</spring-kafka.version>
</properties>
需要说明的是,并非所有在<properties>标签中配置了版本号的依赖都有被启用,其启用与否取决于您是否配置了相应的starter。比如tomcat这个依赖就是spring-boot-starter-web的传递性依赖(下面将会描述到)。
spring-boot-starter-web
Spring Boot提供了许多开箱即用的依赖模块,这些模块都是以spring-boot-starter-XX命名的。比如要开启Spring Boot的web功能,只需要在pom.xml中配置spring-boot-starter-web即可:

因为其依赖于spring-boot-starter-parent,所以这里可以不用配置version。保存后Maven会自动帮我们下载spring-boot-starter-web模块所包含的jar文件。如果需要具体查看spring-boot-starter-web包含了哪些依赖,我们可以选择Maven Goals,输入命令dependency:tree

然后点击Execute即可在IDEA控制台查看到如下信息:
[INFO] com.example:demo:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.3.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:2.3.2.RELEASE:compile
[INFO] | | \- org.springframework:spring-context:jar:5.2.8.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.3.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.3.2.RELEASE:compile
[INFO] | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.13.3:compile
[INFO] | | | \- org.apache.logging.log4j:log4j-api:jar:2.13.3:compile
[INFO] | | \- org.slf4j:jul-to-slf4j:jar:1.7.30:compile
[INFO] | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] | +- org.springframework:spring-core:jar:5.2.8.RELEASE:compile
[INFO] | | \- org.springframework:spring-jcl:jar:5.2.8.RELEASE:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.26:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.3.2.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test:jar:2.3.2.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.3.2.RELEASE:test
[INFO] | +- com.jayway.jsonpath:json-path:jar:2.4.0:test
[INFO] | | +- net.minidev:json-smart:jar:2.3:test
[INFO] | | | \- net.minidev:accessors-smart:jar:1.2:test
[INFO] | | | \- org.ow2.asm:asm:jar:5.0.4:test
[INFO] | | \- org.slf4j:slf4j-api:jar:1.7.30:compile
[INFO] | +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:test
[INFO] | | \- jakarta.activation:jakarta.activation-api:jar:1.2.2:test
[INFO] | +- org.assertj:assertj-core:jar:3.16.1:test
[INFO] | +- org.hamcrest:hamcrest:jar:2.2:test
[INFO] | +- org.junit.jupiter:junit-jupiter:jar:5.6.2:test
[INFO] | | +- org.junit.jupiter:junit-jupiter-api:jar:5.6.2:test
[INFO] | | | +- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO] | | | +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] | | | \- org.junit.platform:junit-platform-commons:jar:1.6.2:test
[INFO] | | +- org.junit.jupiter:junit-jupiter-params:jar:5.6.2:test
[INFO] | | \- org.junit.jupiter:junit-jupiter-engine:jar:5.6.2:test
[INFO] | | \- org.junit.platform:junit-platform-engine:jar:1.6.2:test
[INFO] | +- org.mockito:mockito-core:jar:3.3.3:test
[INFO] | | +- net.bytebuddy:byte-buddy:jar:1.10.13:test
[INFO] | | +- net.bytebuddy:byte-buddy-agent:jar:1.10.13:test
[INFO] | | \- org.objenesis:objenesis:jar:2.6:test
[INFO] | +- org.mockito:mockito-junit-jupiter:jar:3.3.3:test
[INFO] | +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] | +- org.springframework:spring-test:jar:5.2.8.RELEASE:test
[INFO] | \- org.xmlunit:xmlunit-core:jar:2.7.0:test
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.3.2.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-json:jar:2.3.2.RELEASE:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.11.1:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.11.1:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.11.1:compile
[INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.11.1:compile
[INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.11.1:compile
[INFO] | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.11.1:compile
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.3.2.RELEASE:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.37:compile
[INFO] | +- org.glassfish:jakarta.el:jar:3.0.3:compile
[INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.37:compile
[INFO] +- org.springframework:spring-web:jar:5.2.8.RELEASE:compile
[INFO] | \- org.springframework:spring-beans:jar:5.2.8.RELEASE:compile
[INFO] \- org.springframework:spring-webmvc:jar:5.2.8.RELEASE:compile
[INFO] +- org.springframework:spring-aop:jar:5.2.8.RELEASE:compile
[INFO] \- org.springframework:spring-expression:jar:5.2.8.RELEASE:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.623 s
[INFO] Finished at: 2020-08-13T16:46:20+08:00
[INFO] Final Memory: 26M/222M
[INFO] ------------------------------------------------------------------------
上述这些依赖都是隐式依赖于spring-boot-starter-web,我们也可以手动排除一些我们不需要的依赖。
比如spring-boot-starter-web默认集成了tomcat,假如我们想把它换为jetty,可以在pom.xml中spring-boot-starter-web下排除tomcat依赖,然后手动引入jetty依赖:

tips:依赖的坐标可以到上述的spring-boot-dependencies-2.3.2.RELEASE.pom文件里查找。再次运行dependency:tree:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.1.2:tree (default-cli) @ demo ---
[INFO] com.example:demo:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.3.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:2.3.2.RELEASE:compile
[INFO] | | \- org.springframework:spring-context:jar:5.2.8.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.3.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.3.2.RELEASE:compile
[INFO] | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.13.3:compile
[INFO] | | | \- org.apache.logging.log4j:log4j-api:jar:2.13.3:compile
[INFO] | | \- org.slf4j:jul-to-slf4j:jar:1.7.30:compile
[INFO] | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] | +- org.springframework:spring-core:jar:5.2.8.RELEASE:compile
[INFO] | | \- org.springframework:spring-jcl:jar:5.2.8.RELEASE:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.26:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.3.2.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test:jar:2.3.2.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.3.2.RELEASE:test
[INFO] | +- com.jayway.jsonpath:json-path:jar:2.4.0:test
[INFO] | | +- net.minidev:json-smart:jar:2.3:test
[INFO] | | | \- net.minidev:accessors-smart:jar:1.2:test
[INFO] | | \- org.slf4j:slf4j-api:jar:1.7.30:compile
[INFO] | +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:test
[INFO] | | \- jakarta.activation:jakarta.activation-api:jar:1.2.2:test
[INFO] | +- org.assertj:assertj-core:jar:3.16.1:test
[INFO] | +- org.hamcrest:hamcrest:jar:2.2:test
[INFO] | +- org.junit.jupiter:junit-jupiter:jar:5.6.2:test
[INFO] | | +- org.junit.jupiter:junit-jupiter-api:jar:5.6.2:test
[INFO] | | | +- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO] | | | +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] | | | \- org.junit.platform:junit-platform-commons:jar:1.6.2:test
[INFO] | | +- org.junit.jupiter:junit-jupiter-params:jar:5.6.2:test
[INFO] | | \- org.junit.jupiter:junit-jupiter-engine:jar:5.6.2:test
[INFO] | | \- org.junit.platform:junit-platform-engine:jar:1.6.2:test
[INFO] | +- org.mockito:mockito-core:jar:3.3.3:test
[INFO] | | +- net.bytebuddy:byte-buddy:jar:1.10.13:test
[INFO] | | +- net.bytebuddy:byte-buddy-agent:jar:1.10.13:test
[INFO] | | \- org.objenesis:objenesis:jar:2.6:test
[INFO] | +- org.mockito:mockito-junit-jupiter:jar:3.3.3:test
[INFO] | +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] | +- org.springframework:spring-test:jar:5.2.8.RELEASE:test
[INFO] | \- org.xmlunit:xmlunit-core:jar:2.7.0:test
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.3.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.3.2.RELEASE:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.11.1:compile
[INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.11.1:compile
[INFO] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.11.1:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.11.1:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.11.1:compile
[INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.11.1:compile
[INFO] | +- org.springframework:spring-web:jar:5.2.8.RELEASE:compile
[INFO] | | \- org.springframework:spring-beans:jar:5.2.8.RELEASE:compile
[INFO] | \- org.springframework:spring-webmvc:jar:5.2.8.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:5.2.8.RELEASE:compile
[INFO] | \- org.springframework:spring-expression:jar:5.2.8.RELEASE:compile
[INFO] \- org.springframework.boot:spring-boot-starter-jetty:jar:2.3.2.RELEASE:compile
[INFO] +- jakarta.servlet:jakarta.servlet-api:jar:4.0.4:compile
[INFO] +- jakarta.websocket:jakarta.websocket-api:jar:1.1.2:compile
[INFO] +- org.eclipse.jetty:jetty-servlets:jar:9.4.30.v20200611:compile
[INFO] | +- org.eclipse.jetty:jetty-continuation:jar:9.4.30.v20200611:compile
[INFO] | +- org.eclipse.jetty:jetty-http:jar:9.4.30.v20200611:compile
[INFO] | +- org.eclipse.jetty:jetty-util:jar:9.4.30.v20200611:compile
[INFO] | \- org.eclipse.jetty:jetty-io:jar:9.4.30.v20200611:compile
[INFO] +- org.eclipse.jetty:jetty-webapp:jar:9.4.30.v20200611:compile
[INFO] | +- org.eclipse.jetty:jetty-xml:jar:9.4.30.v20200611:compile
[INFO] | \- org.eclipse.jetty:jetty-servlet:jar:9.4.30.v20200611:compile
[INFO] | \- org.eclipse.jetty:jetty-security:jar:9.4.30.v20200611:compile
[INFO] | \- org.eclipse.jetty:jetty-server:jar:9.4.30.v20200611:compile
[INFO] +- org.eclipse.jetty.websocket:websocket-server:jar:9.4.30.v20200611:compile
[INFO] | +- org.eclipse.jetty.websocket:websocket-common:jar:9.4.30.v20200611:compile
[INFO] | | \- org.eclipse.jetty.websocket:websocket-api:jar:9.4.30.v20200611:compile
[INFO] | +- org.eclipse.jetty.websocket:websocket-client:jar:9.4.30.v20200611:compile
[INFO] | | \- org.eclipse.jetty:jetty-client:jar:9.4.30.v20200611:compile
[INFO] | \- org.eclipse.jetty.websocket:websocket-servlet:jar:9.4.30.v20200611:compile
[INFO] +- org.eclipse.jetty.websocket:javax-websocket-server-impl:jar:9.4.30.v20200611:compile
[INFO] | +- org.eclipse.jetty:jetty-annotations:jar:9.4.30.v20200611:compile
[INFO] | | +- org.eclipse.jetty:jetty-plus:jar:9.4.30.v20200611:compile
[INFO] | | +- org.ow2.asm:asm:jar:7.3.1:compile
[INFO] | | \- org.ow2.asm:asm-commons:jar:7.3.1:compile
[INFO] | | +- org.ow2.asm:asm-tree:jar:7.3.1:compile
[INFO] | | \- org.ow2.asm:asm-analysis:jar:7.3.1:compile
[INFO] | \- org.eclipse.jetty.websocket:javax-websocket-client-impl:jar:9.4.30.v20200611:compile
[INFO] \- org.glassfish:jakarta.el:jar:3.0.3:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.511 s
[INFO] Finished at: 2020-08-13T16:51:13+08:00
[INFO] Final Memory: 26M/225M
[INFO] ------------------------------------------------------------------------
可看到tomcat已被替换为了jetty。
spring-boot-maven-plugin
spring-boot-maven-plugin为Spring Boot Maven插件,提供了:
-
把项目打包成一个可执行的超级JAR(uber-JAR),包括把应用程序的所有依赖打入JAR文件内,并为JAR添加一个描述文件,其中的内容能让你用
java -jar来运行应用程序。 -
搜索
public static void main()方法来标记为可运行类。
本文介绍了如何使用http://start.spring.io/创建Spring Boot项目,从生成项目文件、简单演示、打包发布到解析pom.xml文件,特别是讨论了spring-boot-maven-plugin的作用。
689

被折叠的 条评论
为什么被折叠?



