弹簧引导Hello World的实现代码

  

本篇文章是SpringBoot最入门的介绍。我们不借助任何额外的工具,从无到有创建一个弹簧引导的web项目,并运行这个项目。

  

  

归根结底,弹簧引导就只是一个框架,几个jar而已,没什么神奇的。但使用弹簧Initializr创建项目的过程把很多信息屏蔽掉了,这样我们就很难搞清楚弹簧引导的本质是什么。下面仅使用maven从无到有构建一个弹簧引导的web项目。
  先创建一个maven空工程如下所示,项目的名字叫spring-boot-hello。

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;项目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”比;   & lt; modelVersion> 4.0.0      & lt; groupId> com.poype   & lt; artifactId> spring-boot-hello   & lt; version> 1.0 -snapshot      & lt; dependencies>      & lt;/dependencies>   & lt;/project>      

现在这还是一个空的maven项目,我们可以在依赖标签中添加我们需要的依赖,例如添加弹簧引导的依赖。但是弹簧引导为了减少配置,方便我们开发,提供了一个父maven工程spring-boot-starter-parent,我们只要让我们的这个项目继承spring-boot-starter-parent工程,就能减少好多配置。修改我们的POM配置如下:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;项目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”比;   & lt; modelVersion> 4.0.0      & lt; groupId> com.poype   & lt; artifactId> spring-boot-hello   & lt; version> 1.0 -snapshot      & lt; parent>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-parent   & lt; version> 2.1.4.RELEASE   & lt;/parent>      & lt; dependencies>      & lt;/dependencies>   & lt;/project>      

目前我们的这个maven项目还没有导入任何依赖性,这点可以通过执行<代码> mvn依赖性:树>   我们要创建的是一个web项目,所以添加spring-boot-starter-web这个依赖。修改POM配置如下:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;项目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”比;   & lt; modelVersion> 4.0.0      & lt; groupId> com.poype   & lt; artifactId> spring-boot-hello   & lt; version> 1.0 -snapshot      & lt; parent>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-parent   & lt; version> 2.1.4.RELEASE   & lt;/parent>      & lt; dependencies>   & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-web   & lt;/dependency>   & lt;/dependencies>   & lt;/project>      

由于在spring-boot-starter-parent的<代码> dependencyManagement>   

  

再次执行<代码> mvn依赖性:树>         [信息]扫描项目……   (信息)   [信息]- - - - - - - - - - - - - - - - - - - & lt;com。poype: spring-boot-hello祝辞- - - - - - - - - - - - - - - - - - - - - - - -   [信息]建筑spring-boot-hello 1.0快照   [信息]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (jar) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   (信息)   [信息]——maven-dependency-plugin: 3.1.1:树(default-cli) @ spring-boot-hello - - - - - -   [信息]com.poype: spring-boot-hello: jar: 1.0快照   \[信息]- org.springframework.boot: spring-boot-starter-web: jar: 2.1.4.RELEASE:编译   [信息]+ - org.springframework.boot: spring-boot-starter: jar: 2.1.4.RELEASE:编译   [信息]| + - org.springframework.boot: spring-boot: jar: 2.1.4.RELEASE:编译   [信息]| + - org.springframework.boot: spring-boot-autoconfigure: jar: 2.1.4.RELEASE:编译   [信息]| + - org.springframework.boot: spring-boot-starter-logging: jar: 2.1.4.RELEASE:编译   [信息]| | + - ch.qos.logback: logback-classic: jar: 1.2.3:编译   [信息]| | | + - ch.qos.logback: logback-core: jar: 1.2.3:编译   [信息]| | | \ - org.slf4j: slf4j-api: jar: 1.7.26:编译   [信息]| | + - org.apache.logging.log4j: log4j-to-slf4j: jar: 2.11.2:编译   [信息]| | | \ - org.apache.logging.log4j: log4j api: jar: 2.11.2:编译   [信息]| | \ - org.slf4j: jul-to-slf4j: jar: 1.7.26:编译   [信息]| + - javax.annotation: javax.annotation-api: jar: 1.3.2:编译   [信息]| + - org.springframework: spring核心:jar: 5.1.6.RELEASE:编译   [信息]| | \ - org.springframework: spring-jcl: jar: 5.1.6.RELEASE:编译   [信息]| \ - org.yaml: snakeyaml: jar: 1.23:运行时   [信息]+ - org.springframework.boot: spring-boot-starter-json: jar: 2.1.4.RELEASE:编译   [信息]| + - com.fasterxml.jackson.core: jackson-databind: jar: 2.9.8:编译   [信息]| | + - com.fasterxml.jackson.core: jackson-annotations: jar: 2.9.0:编译   [信息]| | \ - com.fasterxml.jackson.core: jackson-core: jar: 2.9.8:编译   [信息]| + - com.fasterxml.jackson.datatype: jackson-datatype-jdk8: jar: 2.9.8:编译   [信息]| + - com.fasterxml.jackson.datatype: jackson-datatype-jsr310: jar: 2.9.8:编译   [信息]| \ - com.fasterxml.jackson.module: jackson-module-parameter-names: jar: 2.9.8:编译   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

弹簧引导Hello World的实现代码