简介
- SpringBoot是由Pivota团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
- 原生开发SpringMVC程序过程:
①.导入坐标
②Web Servlet配置类
③Spring配置类
④设置Controller
- Spring程序缺点
- 配置繁琐
- 依赖设置繁琐
- SpringBoot程序优点
- 自动装配
- 起步依赖(简化依赖配置)
- 辅助功能(内置服务器,…)
SpringBoot入门程序
①创建新模块,选择Spring初始化,并配置模块相关基础信息
②选择当前模块需要使用的技术集
③开发控制器类
@Controller
@RequestMapping("/books")
public class BookController {
@RequestMapping("/{id}")
@ResponseBody
public String getById(@PathVariable Integer id) {
System.out.println("id==>" + id);
return "hello";
}
}
④运行自动生成的Application类
- 最简SpringBoot程序所包含的基础文件
- 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 https://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.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.itheima</groupId>
<artifactId>springboot_01_quickstart_01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- <name>springboot_01_quickstart_01</name>-->
<!-- <description>springboot_01_quickstart_01</description>-->
<properties>
<java.version>1.8</java.version>
</properties>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- Application类
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- Spring程序与SpringBoot程序对比
- 基于idea开发SpringBoot程序需要确保联网能够加载到程序框架结构
- 基于SpringBoot官网创建项目(https://start.spring.io/)
SpringBoot项目快速启动
①对SpringBoot项目进行打包(执行Maven构建指令package)
②执行启动指令
java -jar springboot.jar
- 注意:jar支持命令行启动需要依赖maven插件支持,请确认打包时是否具有SpringBoot对应的maven插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
起步依赖
- starter
- SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
- parent
- 所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
- 实际开发
- 使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供
- 如果发生坐标错误,再指定version(要小心版本冲突)
SpringBoot程序启动
- 启动方式
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- SpringBoot在创建项目时,采用jar的打包方式
- SpringBoot的引导类是项目的入口,运行main方法就可以启动项目
- 使用maven依赖管理变更起步依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
- Jetty比Tomcat更轻量级,可扩展性强(相较于Tomcat),谷歌应用引擎(GAE)已经全面切换为Jetty
基础配置
- SpringBoot提供了多种属性配置方式
- application.properties
server.port=80
- application.yml
server:
port: 81
- application.yam
server:
port: 82
- 配置文件优先级:
- properties > yml > yaml
- 范例:
logging:
level:
# root: debug
# 正常级别--
# root: info
root: warn
自动提示功能小时解决方案
yaml
- yaml(YAML Ain’t Markup Language),一种数据序列化格式
- 优点:
- 容易阅读
- 容易与脚本语言交互
- 以数据为核心,重数据轻格式
- YAML文件扩展名
- .yml(主流)
- .yaml
yaml语法规则
- 大小写敏感
- 属性层级关系使用多行描述,每行结尾使用冒号结束
- 使用缩进表示层级关系,同侧级左侧对齐,只允许使用空格( 不允许使用Tab键),每个空格数量可以不定,只要多一个空格都可以表示多一个层级
- 属性值面前添加空格(属性名与属性值之间使用冒号+空格作为分隔)
- # 表示注释
- 范例:
enterprise:
name: itcast
age: 16
tel: 12345678
- 数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔
- 范例
enterprise:
name: itcast
age: 16
tel: 12345678
subject:
- Java
- 前端
- 大数据
yam读取数据方式
- 使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名…}
server:
port: 81
lesson: 27
enterprise:
name: itcast
age: 16
tel: 12345678
subject:
- Java
- 前端
- 大数据
@Value("${lesson}")
private String lesson;
@Value("${server.port}")
private Integer port;
@Value("${enterprise.subject[0]}")
private String subject_00;
- 封装全部数据到Environment对象
// 可以读取全部配置信息
@Autowired
private Environment environment;
System.out.println(environment.getProperty("lesson"));
System.out.println(environment.getProperty("server.port"));
- 自定义对象封装指定数据
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
private String name;
private Integer age;
private String tel;
private String[] subject;
//自己写setter与getter方法
}
@Autowired
private Enterprise enterprise;
自定义对象封装数据警告解决方案
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
多环境启动
yml格式
#设置启用的环境
spring:
profiles:
active: test
---
#开发环境
server:
port: 80
spring:
profiles: dev
---
#生产环境
server:
port: 81
spring:
profiles: pro
---
# 测试环境
server:
port: 82
spring:
profiles: test
- 推荐格式
spring:
config:
active:
on-profile: pro
#这个不写---
properties文件格式
- 主启动配置文件application.properties
spring.profiles.active=pro
- 环境分类配置文件application-pro.properties
server.port=80
- 环境分类配置文件application-dev.properties
server.port=81
- 环境分类配置文件application-test.properties
server.port=82
命令行启动
- 带参数启动SpringBoot
java -jar SpringBoot.jar --spring.profiles.active=test
java -jar SpringBoot.jar --server.port=88 --spring.profiles.active=test
- 在执行打包操作之前可能要进行clean操作
- 修改字符集:
Maven 与SpringBoot多环境兼容
①Maven中设置多环境属性
<profiles>
<!-- 开发环境-->
<profile>
<id>dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<!-- 生产环境-->
<profile>
<id>pro</id>
<properties>
<profile.active>pro</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 测试环境-->
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
②SpringBoot中引用Maven属性
#设置启用的环境
spring:
profiles:
active: ${profile.active}
---
#开发环境
server:
port: 80
spring:
profiles: dev
---
#生产环境
server:
port: 81
spring:
profiles: pro
---
# 测试环境
server:
port: 82
spring:
profiles: test
③执行Maven打包指令
- Maven指令执行完毕后,生成了对应的包,其中类参与编译,但是配置文件并没有编译,而是复制到包中
- 解决思路:对于源码中非java类的操作要求加载Maven对应的属性,解析${ }占位符
④对资源文件开启默认占位符的解析
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
- Maven打包加载到属性,打包顺利通过
配置文件分类
- SpringBoot 中4级配置文件放置位置:
1级:classpath:application.yml
2级:classpath:config/application.yml
3级:file :application.yml
4级:file :config/application.yml - 官方文档说明:
(2.5.0的springboot有一个bug就是必须在config下创建任意一个子目录)
- 作用:
- 3级与4级留做系统打包后设置通用属性
- 1级与2级用于开发阶段设置通用性