Spring Boot入门21

Spring Boot 简介

  • Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者

Spring Boot发展历程

Spring 1.X时代

  • 在Spring 1.X中,都是通过XML文件配置bean,在项目不断增大的过程中,则需要将XML配置分放到不同的配置文件中,需要频繁的在java类和XML配置文件中切换。

Spring 2.X时代

  • 随着JDK1.5带来的注解支持,Spring 2.X可以使用注解对Bean进行申明和注入,大大的减少了 xml 配置文件,同时也大大简化了项目的开发。
  • 在此时就出现了一个问题到底是使用XML还是注解呢?
      • 应用的基本配置xml 数据源、资源文件的配置
      • 业务开发用注解,Service中注入Bean

Spring 3.X时代

  • 从Spring 3.x 开始提供了Java配置方式,使用Java配置方式可以更好的理解你配置的Bean。

Spring 4.X时代

  • Spring4.x 和 Spring boot 都推荐使用 java 配置的方式。

Spring 5.X时代

  • Spring5.x 是 Java 界首个支持响应式的 Web 框架,是 Spring 的一个重要版本,距离 Spring4.x 差不多四年。在此期间,大多数增强都是在 SpringBoot 项目中完成的,其最大的亮点就是提供了完整的端到端响应式编程的支持(新增 Spring WebFlux 模块)。
  • Spring WebFiux同时支持使用Spring MVC注解声明 Reactive Controller.和传统的MVC Controller 不同,Reactive Controller 操作的是 非阻塞 的 HttpServletRequest 和 HttpSerHttpResponse,而不再是 Spring MVC 里的 HttpServletRequest 和HttpServletResponse。
  • 至此也代表着 Java 正式迎来了响应式异步编程的时代。

Spring Boot的优缺点分析

优点

  • Spring Boot号称秒级搭建项目
  • 对主流开发框架的无配置集成
  • 项目可独立运行,无须外部依赖Servlet容器
  • 提供运行时的应用监控
  • 极大地提高了开发、部署效率
  • 与云计算的天然集成

缺点

  • 版本迭代速度很快,且模块改动大
  • 配置非手动配置,报错时很难定位
  • 可参考的解决方案较少。

搭建第一个Spring Boot应用程序

  • 这里我们使用“idea”java编程语言开发的集成环境.

IDEA -> New Project -> Spring Initializr

  • 新建一个工程
  • IDEA -> New Project -> Spring Initializr
    在这里插入图片描述

点击连接进入设置

在这里插入图片描述

返回工具之后点击下一步

  • Group>组名公司倒叙
  • Artifact>项目命
  • Type>类型
  • Language>语言
  • Packaging>选择jar包
  • Java Version>Java版本
  • Version>版本(1.0.0-…)
  • name >项目名
  • Description>描述
  • Package>包

在这里插入图片描述

设置好基本信息之后点击下一步选择 Spring Boot 版本及 Web 开发所需的依赖

  • Spring Boot的版本和进入连接你选择的版本保持一致
    在这里插入图片描述

保存到指定的文件夹(idea的工作空间)

在这里插入图片描述

工程目录结构解说

在这里插入图片描述

HelloSpringBootApplication.java:程序入口

在这里插入图片描述

static: 静态资源文件目录

templates:模板资源文件目录

application.properties:Spring Boot 的配置文件,实际开发中会替换成 YAML 语言配置(application.yml)

.gitignore:Git 过滤配置文件

pom.xml:Maven 的依赖管理配置文件

  • pom.xml文件详情
    1. parent:继承了 Spring Boot 的 Parent,表示我们是一个 Spring Boot 工程
    2. spring-boot-starter-web:包含了 spring-boot-starter 还自动帮我们开启了 Web 支持
<?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>
<!--继承spring boot (父级pom)-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.fourth</groupId>
<artifactId>spring_boot_hello</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>spring_boot_hello</name>
<description>Demo project for Spring Boot</description>

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

<dependencies>
    <!--web-->
    <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>
</dependencies>
<!--构建时为我们提供了一个spring-boot-maven 帮助我们打包spring boot应用程序-->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

功能演示

  • 在这个功能演示中我将为大家演示Spring Boot框架在无需集群配置的前提下实现功能
  1. 在Spring Boot程序入口下添加RestController注解
  2. 使用RequestMapping注解实现功能
  3. 点击右键选择运行即可。
package com.fourth.spring.boot.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

//boot程序的入口
@SpringBootApplication
@RestController
public class SpringBootHelloApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringBootHelloApplication.class, args);
    }
    @RequestMapping(value = "",method = RequestMethod.GET)
    public String sayHi(){
        return "hello Spring boot";
    }

}

运行

  • 服务器启动后在浏览器输入“locallhost:8080/”即可
    在这里插入图片描述

Spring Boot 单元测试

主要是通过 @RunWith 和 @SpringBootTest 注解来开启单元测试功能

package com.funtl.hello.spring.boot;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.net.URL;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

//主要通过classes = HelloSpringBootApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT让测试装载到SpringBoot的配置
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HelloSpringBootApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloSpringBootApplicationTests {

    @LocalServerPort
    private int port;

    private URL base;

    @Autowired
    private TestRestTemplate template;

    @Before
    public void setUp() throws Exception {
        this.base = new URL("http://localhost:" + port + "/");
    }

    @Test
    public void contextLoads() {
        ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
        assertThat(response.getBody(), equalTo("Hello Spring Boot"));
    }

}

运行它会先启动 Spring Boot 工程,再启动单元测试

快扫描下方二维码每天都有“特干货”

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值