SpringBoot入门

本文将介绍在Maven环境下SpringBoot的使用方法。

Spring

Spring是JVM(Java虚拟机)生态环境里一个提供应用开发需要用到的技术栈整合的框架,是一系列常用代码的抽象提取与整合,其最主要的应用场景就是Web开发

Spring应用场景(Web开发)

  • Web请求路由
  • HTTP请求解析
  • Session管理
  • MVC架框架
  • 依赖注入

Spring Boot

Spring Boot是帮助我们快速使用Spring创建应用的工具和框架,是一个很好的学习Java Web App技术栈的平台,它把Spring开发中使用到的流程和工具集成在一起,然后boot使用

Spring Boot功能

  • 快速开始使用Spring
  • 构建应用 - REST API, WebSocket, Web, Streaming, Tasks, 等等
  • 简化安全
  • 对SQL和NoSQL的丰富支持
  • 内嵌的运行时支持 - Tomcat, Jetty等
  • 提高开发效率的工具
  • 生产环境支持:跟踪,指标,监控
  • 集成IDE

Hello World

Dependencies

要导入Spring框架和SpringBoot工具,IDEA的Maven项目中pom.xml配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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>com.wanzi</groupId>
<artifactId>spring-sample</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>

只要设置auto import dependencies,IDEA就可以在后台下载并处理好依赖,在Maven Project栏Plugins中出现Spring Boot

GreetingControler

  • 定义Web请求的接收和处理
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    package hello;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;

    import java.util.concurrent.atomic.AtomicLong;

    // 告诉Spring,这是一个RESTful API 的 Controller
    @RestController
    public class GreetingController {
    private static final String template = "Hello,%s!";
    private final AtomicLong counter = new AtomicLong(); //原子操作,因为Spring默认是支持多线程的处理请求

    // 当用户访问 /greeting 这个路径的时候,请Spring框架调用这个方法去执行相关逻辑
    @RequestMapping("/greeting")

    //value里的name是键名 //自动将类转换成json,并返回
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World")String name) {
    return new Greeting(counter.incrementAndGet(), String.format(template,name)); // counter += 1 ; return counter;
    }

    /*
    // 直接输出String的方式
    public String greeting(@RequestParam(value = "name", defaultValue = "World")String name) {
    return String.format(template,name);
    }
    */
    }

功能
1.接受用户的请求和用户输入:name
2.解析输入,然后给用户一个json的结构,里面就是Hello,(name)!

框架和库的关系大致如下:
1.框架 – 框架负责调用你的代码
2.库 – 我们去调用库的代码

Greeting

  • 定义处理Web请求的结果对象
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package hello;

    // Spring会自动调用这两个参数的getter方法,去获得成员变量的值,然后构造一个json的返回结果
    public class Greeting {
    private final long id;
    private final String content;

    public Greeting(long id, String content) {
    this.id = id;
    this.content = content;
    }

    // 必须有get,Spring 默认调用有get方法的参数,转换成json
    public long getId() {
    return id;
    }

    public String getContent() {
    return content;
    }

    }

Application

  • 定义一个Spring程序的入口, Spring负责初始化Context
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    package hello;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication // 告诉Spring从这里启动,不可以省略
    public class Application {

    public static void main(String[] args){
    // 启动Spring框架,把控制权交给Spring框架
    SpringApplication.run(Application.class,args);
    }
    }

测试

1.点击Maven Project -> Plugins -> spring-boot -> spring-boot:run启动
2.启动成功后,打开浏览器,地址输入http://localhost:8080/greeting,输出为{“id”:1,”content”:”Hello,World!”}
3.地址输入http://localhost:8080/greeting?name=wanz,则输出为{“id”:2,”content”:”Hello,wanz!”},计数实现
4.把返回对象改为字符串而不是对象,则直接输出Hello,world!

层次结构

1.Spring 接受请求,解析请求
2.属于我们的代码 – 随便写
3.可以读取数据库,做个爬虫

相当于:把结果返回给Spring,Spring再返回给用户