SpringBoot概述,启动依赖,配置文件格式,及其基础配置
SpringBoot概述
-
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
-
Spring程序缺点
- 配置繁琐
- 依赖设置繁琐
-
SpringBoot程序优点
- 自动化配置
- 起步依赖(简化依赖配置)
- 辅助功能(内置服务器,....)
SpringBoot起步依赖
-
starter
-
SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,可以减少手动依赖配置的目的
-
起步依赖的东西
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<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>
好处:无需写版本,无需再去配置更多的依赖
-
-
parent
-
所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以大道减少依赖冲突的目的
-
spring-boot-starter-parent(2.5.0)与spring-boot-start-parent(2.4.6)共计57处坐标版本不同
-
-
实际开发
- 使用任意坐标是,仅书写GAV中的GA,V由SpringBoot提供
- 如果发送坐标错误,再指定version(要小心版本冲突)
-
辅助功能
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
-
启动方式
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); } }
-
SpringBoot在创建项目是,采用jar的打包方式
-
SpringBoot的引导类是项目的入口,运行main方法就可以启动项目
-
使用maven依赖管理变更起步依赖项
<dependencies> <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> </dependencies>
-
Jetty比Tomcat更轻量级,可扩展性更强(相较于Tomcat),谷歌应用引擎(GAE)已经全面切换为Jetty了
-
换技术换起步依赖就行了
配置文件格式
-
修改服务器端口
-
SpringBoot提供了多种属性配置方式
-
application.properties
sever.port=80
-
application.yml(主要)
server: port:81
-
appliction.yaml
server: port:82
-
-
自动提示功能消失解决方案
-
SpringBoot配置文件加载顺序(了解)
- application.properties > application.yml >application.yaml
注意事项:
SpringBoot核心配置文件名为application
SpringBoot内置属性过多,且所有属性集中在一起修改,在使用是,通过提示键+关键字修改属性
-
基础配置
yaml
yaml概览
- YAML(YAML Ain't Markup Language), 一种数据序列化格式
- 优点:
- 容易阅读
- 容易与脚本语言交互
- 以数据为核心,重数据轻格式
- YAML文件拓展名
- .yml(主流)
- .yaml
- yaml语法规则
- 大小写敏感
- 属性多级层次关系使用多行描述,每行结尾使用冒号结束
- 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
- 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔
- #表示注释
- 核心规则:数据前面要加空格与冒号隔开
yaml数组数据
-
数组数据再数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据键空格分隔
enterprise: name: hcx age: 21 tel: 4006154000 subject: -Java -前端 -大数据
yaml数据读取方式
1.使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名}【直接读取】
server:
port: 8080
logging:
level:
root: info
lesson: SpringBoot
enterprise:
name: hcx
age: 21
tel: 4006184000
subject:
- Java
- 前端
- 大数据
@RestController
@RequestMapping("/books")
public class BookController {
@Value("${lesson}")
private String lesson;
@Value("${server.port}")
private Integer port;
@Value("${enterprise.subject[0]}")
private String[] subject_00;
}
2.封装全部数据到Environment对象【封装后读取】
server:
port: 8080
logging:
level:
root: info
lesson: SpringBoot
enterprise:
name: hcx
age: 21
tel: 4006184000
subject:
- Java
- 前端
- 大数据
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private Environment environment;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println(environment.getProperty("lesson"));
System.out.println(environment.getProperty("server.port"));
System.out.println(environment.getProperty("enterprise.age"));
System.out.println(environment.getProperty("enterprise.subject[1]"));
return "hello,spring boot";
}
}
3.自定义对象封装指定数据【封装到类里面后读取】
server:
port: 8080
logging:
level:
root: info
lesson: SpringBoot
enterprise:
name: hcx
age: 21
tel: 4006184000
subject:
- Java
- 前端
- 大数据
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private Environment environment;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println(enterprise);
return "hello,spring boot";
}
}
@Component
@ConfigurationProperties(prefix = "enterprise")
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Enterprise {
private String name;
private Integer age;
private String tel;
private String[] subject;
}
注意事项:
自定义对象封装数据警告解决方案
pom.xml里面加上
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
多环境启动
yaml格式
# 设置启用的环境
spring:
profiles:
active: dev
---
# 生产
spring:
profiles: pro
server:
port: 80
---
# 开发
spring:
profiles: dev
server:
port: 8080
---
# 测试
spring:
profiles: test
server:
port: 8081
# 在springboot2.5中这是一种过时格式
spring:
profiles:
active: pro
---
server:
port: 80
spring:
config:
activate:
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
-
修改端口号并且修改环境
java -jar springboot.jar --server.port=88 --spring.profiles.active=test
-
参数加载优先级
- 详见核心功能 (spring.io)【优先级由上到下,由低到高】
- 默认属性(由设置 指定)。
SpringApplication.setDefaultProperties
@PropertySource
类的注释。请注意,在刷新应用程序上下文之前,不会将此类属性源添加到 中。配置某些属性(如 和 在刷新开始之前读取的属性)为时已晚。@Configuration``Environment``logging.*``spring.main.*
- 配置数据(如文件)。
application.properties
- 仅在 中具有属性的 。
RandomValuePropertySource``random.*
- 操作系统环境变量。
- Java 系统属性 ()。
System.getProperties()
- 来自 的 JNDI 属性。
java:comp/env
ServletContext
初始化参数。ServletConfig
初始化参数。- 属性来自(嵌入在环境变量或系统属性中的内联 JSON)。
SPRING_APPLICATION_JSON
- 命令行参数。
properties
测试的属性。在@SpringBootTest
和测试注释中可用,用于测试应用程序的特定切片。@TestPropertySource
测试上的注释。- 当 devtools 处于活动状态时,目录中的 Devtools 全局设置属性。
$HOME/.config/spring-boot
- 默认属性(由设置 指定)。
- 详见核心功能 (spring.io)【优先级由上到下,由低到高】
-
注意:
- 打包时Maven生命周期先执行clean再去执行package
- 记得修改项目编码字符集为utf-8
- 配置文件较多时记得整理,不要让多余的配置文件干扰到你的验证效果