46.SpringBoot基础(一)

0.学习目标

  • 了解SpringBoot的作用

  • 掌握java配置的方式

  • 了解SpringBoot自动配置原理

  • 掌握SpringBoot的基本使用

  • 了解Thymeleaf的基本使用

1. 了解SpringBoot

在这一部分,我们主要了解以下3个问题:

  • 什么是SpringBoot

  • 为什么要学习SpringBoot

  • SpringBoot的特点

1.1.什么是SpringBoot

https://docs.spring.io/spring-boot/docs/2.0.6.RELEASE/reference/htmlsingle/

SpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品:

我们在springboot概述页面可以看到下面的一段介绍:

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 very little Spring configuration.

翻译一下:

Spring Boot你只需要“run”就可以非常轻易的构建独立的、生产级别的spring应用。
我们为spring平台和第三方依赖库提供了一种固定化的使用方式,使你能非常轻松的开始开发你的应用程序。大部分Spring Boot应用只需要很少的配置。

其实人们把Spring Boot称为搭建程序的`脚手架`。其最主要作用就是帮我们快速的构建庞大的spring项目,并且尽可能的减少一切xml配置,做到开箱即用,迅速上手,让我们关注于业务而非配置。

我们可以使用SpringBoot创建java应用,并使用java –jar 启动它,就能得到一个生产级别的web工程。

1.2.为什么要学习SpringBoot

java一直被人诟病的一点就是臃肿、麻烦。当我们还在辛苦的搭建项目时,可能Python程序员已经把功能写好了,究其原因主要是两点:

  • 复杂的配置

    项目各种配置其实是开发时的损耗, 因为在思考 Spring 特性配置和解决业务问题之间需要进行思维切换,所以写配置挤占了写应用程序逻辑的时间。

  • 混乱的依赖管理

    项目的依赖管理也是件吃力不讨好的事情。决定项目里要用哪些库就已经够让人头痛的了,你还要知道这些库的哪个版本和其他库不会有冲突,这也是件棘手的问题。并且,依赖管理也是一种损耗,添加依赖不是写应用程序代码。一旦选错了依赖的版本,随之而来的不兼容问题毫无疑问会是生产力杀手。

而SpringBoot让这一切成为过去!

1.3.SpringBoot的特点

Spring Boot 主要特征是:

  • 创建独立的spring应用程序

  • 直接内嵌tomcat、jetty和undertow(不需要打包成war包部署)

  • 提供了固定化的“starter”配置,以简化构建配置

  • 尽可能的自动配置spring和第三方库

  • 提供产品级的功能,如:安全指标、运行状况监测和外部化配置等

  • 绝对不会生成代码,并且不需要XML配置

总之,Spring Boot为所有 Spring 的开发者提供一个开箱即用的、非常快速的、广泛接受的入门体验

更多细节,大家可以到官网查看。

2.快速入门

接下来,我们就来利用SpringBoot搭建一个web工程,体会一下SpringBoot的魅力所在!

环境要求:

https://docs.spring.io/spring-boot/docs/2.0.6.RELEASE/reference/htmlsingle/#getting-started-system-requirements

2.1.创建工程

我们先新建一个空的demo工程,如下:

创建以moduel:

填写坐标信息:

目录结构:

创建完成后的目录结构:

2.2.引入依赖

看到这里很多同学会有疑惑,前面说传统开发的问题之一就是依赖管理混乱,怎么这里我们还需要管理依赖呢?难道SpringBoot不帮我们管理吗?

别着急,现在我们的项目与SpringBoot还没有什么关联。SpringBoot提供了一个名为spring-boot-starter-parent的工程,里面已经对各种常用依赖(并非全部)的版本进行了管理,我们的项目需要以这个项目为父工程,这样我们就不用操心依赖的版本问题了,需要什么依赖,直接引入坐标即可!

2.2.1.添加父工程坐标

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

2.2.2.添加web启动器

为了让SpringBoot帮我们完成各种自动配置,我们必须引入SpringBoot提供的自动配置依赖,我们称为启动器。因为我们是web项目,这里我们引入web启动器:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
</dependencies>

需要注意的是,我们并没有在这里指定版本信息。因为SpringBoot的父工程已经对版本进行了管理了。 这个时候,我们会发现项目中多出了大量的依赖:

2.2.3.完整pom

<?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>cn.yh.springboot</groupId>
    <artifactId>yh-springboot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 所有的springboot的工程都以spring父工程为父工程 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

2.3.启动类

代码:

//@SpringBootApplication
@EnableAutoConfiguration 
@ComponentScan
public class SpringbootDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootDemoApplication.class);
    }
}

2.4.编写controller

接下来,我们就可以像以前那样开发SpringMVC的项目了!

我们编写一个controller:

@Controller
public class DemoController {
    @RequestMapping("/first")
    @ResponseBody
    public String first(){
        return "this is my first spring boot project!";
    }
}

@RestController
public class DemoController {

    @GetMapping("/first")
    public String first(){
        return "this is my first spring boot project!";
    }
}

2.5.启动测试

接下来,我们运行main函数,查看控制台:

并且可以看到监听的端口信息:

  • 1)监听的端口是8080

  • 2)SpringMVC的映射路径是:/

  • 3)/first路径已经映射到了DemoController中的first()方法

打开页面访问:http://localhost:8080/first

2.5.详解

入门工程中:pom.xml里引入了启动器的概念以@EnableAutoConfiguration注解。

2.5.1.启动器

为了让SpringBoot帮我们完成各种自动配置,我们必须引入SpringBoot提供的自动配置依赖,我们称为启动器。spring-boot-starter-parent工程将依赖关系声明为一个或者多个启动器,我们可以根据项目需求引入相应的启动器,因为我们是web项目,这里我们引入web启动器:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

需要注意的是,我们并没有在这里指定版本信息。因为SpringBoot的父工程已经对版本进行了管理了。

这个时候,我们会发现项目中多出了大量的依赖:

这些都是SpringBoot根据spring-boot-starter-web这个依赖自动引入的,而且所有的版本都已经管理好,不会出现冲突。

2.5.2.@EnableAutoConfiguration

关于这个注解,官网上有一段说明:

Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined.

简单翻译以下:

开启spring应用程序的自动配置,SpringBoot基于你所添加的依赖和你自己定义的bean,试图去猜测并配置你想要的配置。比如我们引入了spring-boot-starter-web,而这个启动器中帮我们添加了tomcatSpringMVC的依赖。此时自动配置就知道你是要开发一个web应用,所以就帮你完成了web及SpringMVC的默认配置了!

总结,SpringBoot内部对大量的第三方库或Spring内部库进行了默认配置,这些配置是否生效,取决于我们是否引入了对应库所需的依赖,如果有那么默认配置就会生效。

所以,我们使用SpringBoot构建一个项目,只需要引入所需依赖,配置就可以交给SpringBoot处理了。

2.5.2.@ComponentScan

spring框架除了提供配置方式的注解扫描<context:component-scan />,还提供了注解方式的注解扫描@ComponentScan

在TestApplication.class中,使用@ComponentScan注解:

@EnableAutoConfiguration
@ComponentScan
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

}

重新启动,访问show或者show2:

我们跟进该注解的源码,并没有看到什么特殊的地方。我们查看注释:

大概的意思:

配置组件扫描的指令。提供了类似与<context:component-scan>标签的作用

通过basePackageClasses或者basePackages属性来指定要扫描的包。如果没有指定这些属性,那么将从声明这个注解的类所在的包开始,扫描包及子包

而我们的@ComponentScan注解声明的类就是main函数所在的启动类,因此扫描的包是该类所在包及其子包。一般启动类会放在一个比较浅的包目录中。

2.5.3.@SpringBootApplication

我们现在的引导类中使用了@EnableAutoConfiguration和@ComponentScan注解,有点麻烦。springboot提供了一种简便的玩法:@SpringBootApplication注解

使用@SpringBootApplication改造TestApplication:

@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

}

点击进入,查看源码:

发现@SpringBootApplication其实是一个组合注解,这里重点的注解有3个:

  • @SpringBootConfiguration

  • @EnableAutoConfiguration:开启自动配置

  • @ComponentScan:开启注解扫描

2.5.4.@SpringBootConfiguration

@SpringBootConfiguration注解的源码:

我们继续点击查看源码:

通过这段我们可以看出,在这个注解上面,又有一个@Configuration注解。通过上面的注释阅读我们知道:这个注解的作用就是声明当前类是一个配置类,然后Spring会自动扫描到添加了@Configuration的类,并且读取其中的配置信息。而@SpringBootConfiguration是来声明当前类是SpringBoot应用的配置类,项目中只能有一个。所以一般我们无需自己添加。


已有 0 条评论

    欢迎您,新朋友,感谢参与互动!