Spring Boot 之 Hello World

Spring Boot

什么是 Spring Boot ?

官网是这样概述的

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.

Spring Boot 可以使得创建直接运行的、独立的、生产级的基于 Spring 的应用程序变得很容易,项目运行的更快。

我们对于 Spring 平台采用了一个自行设计的视图和第三方库来让你便于快速启动项目,这样基于 Spring Boot 的项目仅仅需要极少的 Spring 框架的配置。

因为我们知道,在没有 Spring Boot 的产生的情况下,SSM 整合的需要指定各种配置,比如 Spring MVC 和 Spring 的配置文件要分开等等。

为了解决在开发上繁琐的各种配置, Spring Boot 出来了,秉承简化开发,约定大于配置,you can “just run” ,能够迅速开发 web 应用,仅需几行代码就可以开发一个 http 接口。

也可以说,Spring Boot 是基于 Spring 开发,不是用来替代 Spring 的解决方案,,而是和 Spring 框架紧密结合用于提升 Spring 开发者体验的工具。

特性

  1. 创建单机版的 Spring 程序
  2. 直接内嵌了 Tomcat、Jetty 或者 Undertow 应用服务器(无需再部署 war包);
  3. 开箱即用,提供各种默认配置来简化项目配置
  4. 可以随时自动配置Spring和第三方类库;
  5. 完全没有冗余代码生成,并且无需XML配置。
  6. 为所有 Spring 开发者更快的入门

Hello,World

有句话很经典,做开发,看版本,我的环境是:

  • java version “1.8.0_181”
  • Maven-3.6.1
  • Spring Boot 2.3.3
  • 开发工具 IDEA 2020

创建项目

方式一:

通过 Spring 官网提供的 Quickstart Your Project,打开网站 https://start.spring.io/

,在该页面场景一个 Web 工程

  1. 填写项目信息
  2. 点击 ADD DEPENDENCIES 选择 WEB 模块
  3. 选择 Maven 工程和 Java 语言,选择 Spring Boot 版本
  4. 点击”Generate Project“按钮生成项目;下载此项目
  5. 解压下载下来的压缩包,使用 IDEA 导入工程

方式二:

  1. 创建一个新项目

  2. 选择spring initalizr , 可以看到默认就是去官网的快速构建工具那里实现

  3. 填写项目信息

  4. 选择初始化的组件(初学勾选 Web 即可)

  5. 填写项目路径

  6. 等待项目构建成功

解析项目结构

导入 IDEA 之后,就是一个典型的 Maven 工程结构,我们可以只留下 src 文件夹和 pom.xml 文件。点开 DemoApplication 类,并且 run ,即可启动一个简单的 Web 的 Spring Boot 程序。


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.3.RELEASE)

2020-08-22 23:56:33.168  INFO 10032 --- [           main] c.e.springdemo.demo.DemoApplication      : Starting DemoApplication on DESKTOP-MT8IMA9 with PID 10032 (D:\WorkSpace\IdeaWorkSpace\SpringBoot_Test01\SpringBoot01\target\classes started by 15153 in D:\WorkSpace\IdeaWorkSpace\SpringBoot_Test01)
2020-08-22 23:56:33.174  INFO 10032 --- [           main] c.e.springdemo.demo.DemoApplication      : No active profile set, falling back to default profiles: default
2020-08-22 23:56:34.991  INFO 10032 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-08-22 23:56:34.999  INFO 10032 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-22 23:56:34.999  INFO 10032 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-22 23:56:35.084  INFO 10032 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-22 23:56:35.084  INFO 10032 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1804 ms
2020-08-22 23:56:35.583  INFO 10032 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-22 23:56:36.002  INFO 10032 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-22 23:56:36.015  INFO 10032 --- [           main] c.e.springdemo.demo.DemoApplication      : Started DemoApplication in 3.655 seconds (JVM running for 5.3)
Hello World,Spring Boot

一个简单的 Spring Boot web 程序就启动了,从上述信息中,o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 告诉了我们默认的 web 服务器是 Tomcat,端口号为 8080

解析 pom.xml

	<!--父依赖-->
	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
   <!-- ... -->
   <!--Java 版本-->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
    	<!-- Web 启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		<!-- test 启动器 -->
        <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>
    </dependencies>
	<!--Maven 打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

从上面可以看出:

  1. 我们的依赖不需要指定版本,Spring Boot 通过定义一个父依赖来帮我们自动导入各种依赖的所需版本等各种信息。
  2. 通过一个个启动器,来自动导入需要的依赖,例如当需要 Web 支持的时候,只需要导入一个 spring-boot-starter-web 启动器,即可拥有 Web 开发环境。

打开源码文件,在 spring-boot-dependencies-x.x.x.RELEASE.pom 文件中,就能够看到 Spring Boot 为我们已经定义好的,适合的各种依赖的版本。

编写测试接口

在 DemoApplication 类所在包及其子包下(注意,这里必须是启动类 DemoApplication 所在包及其子包下,否则无法扫描到我们编写的接口),建立一个包 Controller,并且新建类 TestController.java。示例代码如下:

@RestController
public class TestController {

    @GetMapping("hello")
    public String hello(){
        return "Hello World";
    }
}

启动 Spring Boot 程序,在浏览器输入 localhost:8080/hello 我们就可以看到下面结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值