SpringBoot简介与"Hello World"
SpringBoot是近年来比较火的一门技术,越来越多的公司已经在使用,很多公司招聘也会要求求职者掌握,作为一个与时俱进的程序员,为了提高我们的姿势水平,也得赶紧学起来啊,本篇博客简要介绍SpringBoot及其特性,还有怎么样快速开始一个SpringBoot项目。
什么是SpringBoot
引用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.
大致意思就是说,SpringBoot能够让我们创建一个基于Spring的应用变得更加容易,我们只需要很少的配置就能开始一个Spring的应用,避免了搭建框架初期大量的配置及遇到的麻烦。
总的来说
SpringBoot是Spring旗下的一款框架,用来帮助我们简化spring应用开发,通过整合整个Spring技术栈,常用的如:SpringCloud,Spring Security,Spring Data,采用约定大于配置的方式,使我们基于少量配置就能快速构建一个Spring的企业级应用。
这样看来,SpringBoot并不是一个全新的东西,它只是帮我们整合了开发当中所需要的各种技术,在没使用SpringBoot之前,我们创建一个web工程,需要编写大量的xml配置文件,比如说ssm,我们要配置bean,数据源,事务等一大堆东西,稍微一不注意出错了,项目就跑不起来,然后再去排错…费时费力,而现在有了SpringBoot我们就可以避免这些烦人的配置了。
SpringBoot的优点
- 快速创建独立的Spring应用
- 直接嵌入的Servlet容器(Tomcat、Jetty或Underow),无需部署war文件
- 尽可能自动配置Spring和第三方库,提供固定的“starter”依赖项以简化Maven构建配置
- 提供生产就绪的特性,如度量、运行状况检查和外部化配置
- 开箱即用,没有代码生成,也无需 XML 配置。
SpringBoot的Hello World
下面我们通过IDEA创建一个简单的demo,可以直接体会到SpringBoot的强大。
1.File >> New >> Project,在弹出的窗口选择Spring Initializr
2.填写项目相关信息,我这里就采用默认的
3.选择web模块
可以看到,除了web模块外,还有很多其他的如sql,nosql,中包含了mybatis和redis,点选可以自动引入相关的依赖,且SpringBoot已经帮我们做好了配置。
4.创建完成后,项目目录结构如下(创建好后别忘了点左下角的Enable Auto-Import,自动导入依赖):
5.项目创建好后,接下来我们实现一个功能,浏览器发送请求/hello,服务器接收请求并响应hello。
在com.example.demo下新建一个controller包,并写一个HelloController类。
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
return "hello";
}
}
可以看到都是springmvc的功能。
6.启动运行
运行DemoApplication这个类的主方法
启动完成后,打开浏览器访问http://localhost:8080/hello。
可以看到,controller成功接收到请求并响应。这里有的同学可能会问了,Tomcat都没有,怎么能够访问的呢,其实SpringBoot是使用的内嵌tomcat,并采用默认的配置帮我们启动,这也是它的一大亮点之一,我们不再需要将项目打包成war再去部署了,我们可以将其直接打包成jar,以java -jar命令行的方式直接运行。
Hello World细节探究
pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- SpringBoot的web模块启动器,还有其它各种启动器,如需要mybatis,redis,则需要引入mybatis和redis的starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot单元测试相关的starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Maven的插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
这是springboot应用的父项目,我们点进去可以发现它又依赖父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
那我们在进到spring-boot-dependencies里面一探究竟
可以看到它定义了许多依赖的版本。这个其实就是SpringBoot的版本仲裁中心,它来真正管理SpringBoot应用里面的所有依赖版本,以后我们导入依赖默认是不需要写版本的(没有在dependencies里面管理的依赖自然需要声明版本号)。
2.启动器
<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>
</dependencies>
spring-boot-starter-web, spring-boot-starter-test:这个叫做spring-boot场景启动器;它帮我们导入了web模块以及测试模块正常运行所依赖的组件;
SpringBoot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器。我们还可以在官方文档中看到还有其他各种场景的启动器,相关链接:starter文档
点击pom都是可以看到各种场景的详细依赖的。
启动类
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
这个类就一个主方法,@SpringBootApplication标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用,至于这个注解的内部实现,我们后面在慢慢探索。SpringApplication.run(DemoApplication.class, args)将启动类作为参数传进来,即启动应用,当然我们可以将启动器写在别处,只要参数传递正确,运行run方法即可。
SpringBoot的配置文件
看了上面是不是感觉使用SpringBoot比我们传统的开发精简多了,之前使用springmvc的话我们还需要自己配置前端控制器,包扫描器等。那么SpringBoot是不是完全不用配置?其实不是的,在我们平时开发中,肯定会做一些自己的定制和配置,SpringBoot使用一个全局的配置文件,配置文件名是固定的;
•application.properties
•application.yml
SpringBoot支持这两种格式的配置文件,比如说,我们想设置访问项目时的context-path,以及设置访问的端口,我们可以添加如下配置
server.port=8081
server.servlet.context-path=/springboot
重新启动,在访问http://localhost:8081/springboot/hello
除此之外,我们还可以在其中配置数据源,视图解析器,mybatis相关配置等,只要有用到能进行相关配置的组件都能够在其中配置。
SprignBoot底层将我们用到组件的配置都封装成properties对象,并跟我们的配置文件绑定起来,要看配置文件中能配什么,只需要看对应properties中有哪些属性就可以了。