springboot系列01-入门篇

Spring Boot是什么    

  Spring Boot 是由 Pivotal 团队提供的全新框架,目的是用来简化新 Spring 应用的初始搭建以及开发过程。框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。就是 Spring Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 Maven 整合了所有的 Jar 包,Spring Boot 整合了所有的框架。

 

使用Spring Boot有什么好处

简单、快速、方便!

对比,搭建一个 Spring Web 项目的时候需要怎么做

  • 1)配置 web.xml,加载 Spring 和 Spring mvc

  • 2)配置数据库连接、配置 Spring 事务

  • 3)配置加载配置文件的读取,开启注解

  • 4)配置日志文件

  • 配置完成之后部署 servlet容器

使用springboot,只需要非常少的几个配置就可以迅速方便的搭建起来一套 Web 项目

快速入门

  • 1、访问 http://start.spring.io

  • 2、选择构建工具 Maven Project、Java、Spring Boot 版本 

  • 3、点击 Generate 下载项目压缩包

  • 4、解压后,使用 Idea 导入项目,File -> New -> Model from Existing Source.. -> 选择解压后的文件夹 -> OK,选择 Maven -> 完成 

  • 5、如果使用的是 Eclipse,Import -> Existing Maven Projects -> Next -> 选择解压后的文件夹 -> 完成

Spring Boot 的基础结构共三个文件:

  • src/main/java 程序开发以及主程序入口

  • src/main/resources 配置文件

  • src/test/java 测试程序

 Spring Boot 启动

1、pom.xml模块

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

 添加支持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> 
   </dependency>

 

pom.xml 文件中默认有两个模块:

  • spring-boot-starter :核心模块,包括自动配置支持、日志和 YAML,如果引入了 spring-boot-starter-web web 模块可以去掉此配置,因为 spring-boot-starter-web 自动依赖了 spring-boot-starter

  • spring-boot-starter-test :测试模块,包括 JUnit、Hamcrest、Mockito

2、编写 Controller 内容:

package com.example.demo.controller;

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

@RestController
public class HelloWorldController {
    
    @RequestMapping("/hello")
    public String index() {
        return "Hello World";
    }
}

 

@RestController 就是 Controller 里面的方法都以 json 格式输出,不用再写什么 jackjson 配置

 

3、启动主程序DemoApplication,打开浏览器访问 http://localhost:8080/hello

看到效果

 

4、另编写单元测试

在src/test/下编写

package com.example.demo;

import com.example.demo.controller.HelloWorldController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloTest {
    private MockMvc mvc;

    @Before
    public void setUp() throws Exception {
        mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build();
    }

    @Test
    public void getHello() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().string(equalTo("Hello World")));
    }
}

 

开发环境调试

springBoot对调试支持很好,修改之后可以实时生效,需要添加以下的配置pom.xml:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional>
</dependency>
<build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <fork>true</fork> <!-- 重要 -->
        </configuration>
      </plugin>
    </plugins>
</build>

 该模块在完整的打包环境下运行的时候会被禁用!

 

posted @ 2022-01-13 17:04  IT6889  阅读(34)  评论(0编辑  收藏  举报