一、SpringBoot简介
1.什么是SpringBoot
产生背景:Spring开发比较繁琐,配置文件很多,部署流程复杂,整合第三方框架难度大。这会降低开发效率
SpringBoot是一个简化Spring应用创建和开发的框架
整合了整个Spring技术栈,是JavaEE开发一站式解决方案
2.为什么使用SpringBoot
优点:
- 可以快速构架Spring项目,并与主流框架进行集成
- 内置Servlet容器,不需要手动部署war包
- 使用starter管理依赖并进行版本控制
- 大量自动配置,简化开发
- 提供准生产环境的运行时监控
- 不需要XML文件
二、第一个SpringBoot程序
1.操作步骤
步骤:
1.1 创建一个Maven的jar工程
传统的应用需要创建web工程,然后将应用打成war包,然后部署在容器中
而SpringBoot只需要打成一个jar包,其中内置了tomcat
1.2 导入SpringBoot相关依赖
<?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.ly</groupId>
<artifactId>springboot01-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
</parent>
<name>springboot01-helloworld</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
</build>
</project>
1.3 创建Controller
package com.ly.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Author: LuYi
* Date: 2019/10/27 11:05
* Description: 描述
*/
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
return "Hello World";
}
}
1.4 创建启动类
package com.ly;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Author: LuYi
* Date: 2019/10/27 11:05
* Description: 使用@SpringBootApplication将类标注成SpringBoot应用
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
默认会扫描@SpringBootApplication注解所在的包及其子包,也可使用@ComponentScan("com.ly.controller")注解进行指定
1.5 打包
<!--该插件可以将应用打包成一个可执行的jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
添加该插件,将应用打成可执行的jar包, 执行:java -jar jar文件
2. 分析HelloWorld
2.1 POM文件
- 父工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
</parent>
- 父工程的父工程:用来管理SpringBoot应用中依赖的版本,进行版本控制
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
- 依赖:通过starter指定依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- SpringBoot提供了很多starter(启动器),分别对应了不同的应用场景,当在项目中引入这些starter时,相应场景的依赖就会被导入进来
2.2 启动类
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
- @SpringBootApplication
- 标注在类上,表示这个类是SpringBoot的启动类,通过该类的Main方法启动SpringBoot应用
- @SpringBootConfiguration
- 标注在类上,表示这个类是SpringBoot的配置类
- 层级关系:SpringBootConfiguration——>@Configuration——>@Component
@Configuration:标注在类上,表示这个类是Spring的配置类,相当于XML配置文件
- @EnableAutoConfiguration
- 开启自动配置功能,简化了以前繁琐的配置
- SpringBoot在启动时会在/META-INF/spring.factories中EnableAutoConfiguration指定的值,将这些值作为自动配置类添加到容器中,这些自动配置类会帮我们完成很多配置工作。
- @ComponentScan
- 标注在类上,指定要扫描的包及其子包
三、快速创建SpringBoot项目
1.简介
使用Spring initializer快速构建SpringBoot项目
2. 基本操作
- pom文件和主程序类自动生成,直接写业务逻辑即可
- resources文件夹的目录结构
|-static 存放静态资源,如js,css,images
|-template 存放模板引擎,如freemarker、thymeleaf等
|-application.properties SpringBoot应用的配置文件,可以修改默认设置
四、配置文件
1.简介
SpringBoot的默认全局配置文件有两种:
- application.properties
- application.yml
文件名固定,存放在classpath:/或classpath:/config/目录下
可以修改Spring Boot默认配置,具体参考: http://docs.spring.io/spring-boot…
注意:SpringBoot2.0和1.0的配置有区别,有的配置项已被删除
2.YAML用法
2.1 简介
YAML不是一种标记语言,YAML是专门用来写配置文件的,它以数据为中心,简介强大,比xml和properties更适合做配置文件
YAML文件以.yml或.yaml为后置名
2.2 application.yml
server:
port: 8081 #写法:key: value 冒号后面必须有空格
servlet:
context-path: /springboot03/
2.3 语法规则
- 大小写敏感
- 使用缩进表示层级关系
- 缩进时不允许使用Tab键
- 缩进的空格数目不重要,但是要与对应的层级的左侧对齐
- #表示注释
2.4 基本用法
YAML支持的数据结构有三种:
- 字面量:单个的,不可再分的值(字符串、数字、boolean值)
- 对象:键值对集合
- 数组:一组按次序排列的值
三种数据结构的用法:
1.字面量:普通的值,如数字、字符串、布尔值
number: 12.5
str: hello
name: 'tom cruise' #如字符串包含空格及特殊字符需要使用 引号 引起来
name: 'tom \n cruise' #不会对特殊字符进行转义 结果为:tom 换行 cruise
name: "tom \n cruise" #对特殊字符进行转义,会作为普通字符输出, 结果为 tom \n cruise
- 对象,也成为映射Map,包含属性和值
# 写法1:换行写
user:
name: tom
age: 20
sex: male
# 写法2:行内写法
user: {name: tom, age: 20, sex: male}
- 数组,如List、Set等
# 写法1: 一组短横线开头的行
names:
- tom
- jack
- alice
# 写法2: 行内写法
name: {tom,jack,alice}
3. 为属性注入值
通过加载配置文件,为类中的属性注入值
3.1 编写application.yml
user:
username: admin
age: 21
status: true
birthday: 2019/2/14
address:
province: 黑龙江省
city: 哈尔滨市
lists:
- list1
- list2
- list3
maps: {k1: v1,k2: v2}
3.2 创建实体类
User
package com.luyi.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Author: LuYi
* Date: 2019/10/27 13:49
* Description: 通过加载配置文件为当前类中的属性注入值
*/
// 必须将当前类加入到容器
@Component
// 默认读取全局配置文件获取值,当前类中的所有属性与 user 进行绑定
@ConfigurationProperties(value = "user")
public class User {
private String username;
private Integer age;
private Boolean status;
private Date birthday;
private Address address;
private List<String> lists;
private Map<String, Object> maps;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public