【Java从0到架构师】SpringBoot - 入门_配置文件_YAML
SpringBoot - 入门_配置文件_YAML
- SpringBoot - 入门
- @SpringBootApplication
- 可运行 jar - spring-boot-maven-plugin
- 热部署 - devtools
- 配置文件 - application
- 配置文件默认加载路径
- 运行参数、VM 选项
- 配置文件的内容
- YAML
- 提高开发效率的依赖
- 属性绑定 - @ConfigurationProperties
- 构造方法绑定 - @ConstructorBinding
- 宽松绑定 (Relaxed Binding) - 概念
- 拆分配置文件
- banner
Java 从 0 到架构师目录:【Java从0到架构师】学习记录
Gitee 代码:https://gitee.com/szluyu99/mj_java_frame/tree/master/04_SpringBoot
JavaEE 开发存在的问题(即使使用了 SSM)
- 配置繁多、部署复杂、集成第三方库并不简单
- SpringBoot 可以很好地解决上述问题
官网:https://spring.io/projects/spring-boot
- 本课程使用的版本:2.3.4.RELEASE
SpringBoot 的特点
- 内置了很多常用配置,让 JavaEE 开发更加简单
- 项目可以独立运行,无须额外依赖 web 容器(内置了 web 容器)
- …
SpringBoot - 入门
入门项目:Gitee 代码
pom.xml:
<!-- 继承父项目,里面有各种库的版本号 -->
<parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.3.4.RELEASE</version>
</parent><!-- 依赖 -->
<dependencies><!-- web项目(已经集成了SpringMVC中很多的常用库) --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>
controller:
@RestController
public class TestController {@GetMapping("/test")public String test() {return "Hello SpringBoot";}
}
程序入口:
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
执行 main 方法后,在浏览器输入:http://localhost:8080/test
@SpringBootApplication
@SpringBootApplication
中包含了 3 个注解:
@EnableAutoConfiguration
:可以根据 maven 依赖自动构建相关环境
比如为 spring-boot-starter-web 构建 web 容器环境等@componentScan
:默认会扫描当前包以及子包中的所有类@SpringBootConfiguration
从 SpringBoot 2.2.1 开始,被 SpringBoot 扫描到的@Component
不用添加@Configuration
可运行 jar - spring-boot-maven-plugin
如果希望通过 mvn package
打包出一个可运行的 jar,需要增加一个插件:
<build><finalName>sb_hw</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build>
也可以使用插件的 run 功能,直接运行 SpringBoot 应用:
热部署 - devtools
增加依赖( Debug 模式下,它可以监控 classpath 的变化)
<!-- 热部署 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId>
</dependency>
重新编译项目(2 种方式)
- 手动编译(Build Project、Build Module)
- 自动编译
需要注意的是:使用了 spring-boot-devtools 后可能会执行 2 次 main 方法
- 不用在意,因为 devtools 仅限于开发调试阶段使用
需要使用热部署还需要在 Idea 开启以下设置:
- Build project automatically:
- Ctrl + Shift + Alt + /
以上配置的效果为: 当 Idea 失去焦点时,会进行自动编译(会有点延迟)
配置文件 - application
配置文件默认加载路径
SpringBoot 默认会去加载一个应用程序配置文件,文件名为 application
应用程序配置文件(Application Property Files)
application 配置文件默认可以存放的位置如下所示(按照优先级从高到低排序)
- file:./config/
- file:./config/*/
- file:./
- classpath:/config/
- classpath:/
注: file:./ 代表项目根路径(jar 包), classpath:/ 代表 resources 目录
最常见的做法就是:classpath:/,即直接在 resources 下放配置文件
注:application 配置文件的内容可以直接通过 @Value
进行注入
注意:Map 类型的注入比较特殊,后面有介绍
如果是其他配置文件需要
@PropertySource
引入配置文件
- 由于 application 在 SpringBoot 项目中必然会被加载,因此无需引入,可以直接注入
运行参数、VM 选项
可以通过运行参数或 VM 选项,指定配置文件的文件名、位置:
java -jar myproject.jar --spring.config.name=mj
java -jar myproject.jar --spring.config.location=F:/mj.properties
在 Idea 中启动配置中可以配置运行参数、VM 选项:
- 通过运行参数或 VM 选项,可以覆盖配置文件的内容
运行参数会传递给 main 方法的 args 参数:
--spring.config.name=mj
是运行参数
System.getProperty、Ststem.getProperties 可以获取 VM 选项:
-Dspring.config.name=mj
是 VM 选项
配置文件的内容
在 application 配置文件中,关于服务器的配置内容都可以从下面找到:
- spring-boot-autoconfigure.jar / META-INF / spring.factories
- ServletWebServerFactoryAutoConfiguration
- ServerProperties
YAML
YAML(YAML Ain’t a Markup Language,YAML 不是一种标记语言)
- SpringBoot 更推荐使用 YAML 作为配置文件
- 文件拓展名是 .yml
- YAML 使用空格或 TAB 作为层级缩进
- 冒号
:
与后面紧跟的内容之间必须要有空格或 TAB - 字符串可以不用加引号。如果有转义字符,可以使用双引号括住字符串
# \n会作为字符串输出
name1: m\nj
# \n会被转移为换行输出
name2: "m\nj"
提高开发效率的依赖
为了更快捷的完成绑定工作,可以添加 2 个依赖:
<!-- 配置文件属性名提示 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId>
</dependency><!-- 在编译期间帮助生成Getter、Setter等代码 -->
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope>
</dependency>
注意:
- spring-boot-configuration-processor 需要代码重新编译后,才会生成提示
- Lombok 仅仅在编译期间使用即可,所以 scope 设置为 provided 即可
仅仅引入 Lombok 依赖,虽然使用了 @Data
会自动生成 Getter、Setter 方法,但是在开发期间不会有代码提示,需要代码提示还需要安装 Lombok 插件:Lombok官网
属性绑定 - @ConfigurationProperties
属性绑定(JavaBean properties binding)
使用了 @ConfigurationProperties("student")
指定了配置文件中的前缀,通过这个注解可以将配置文件中的值直接读取到指定类中(类名与前缀对应):
使用了 @EnableConfigurationProperties
后,Student 就不用就加 @Component
了
再倒回去看 ServletWebServerFactoryAutoConfiguration,可以理解的更加深刻
@ConfigurationProperties
也可以用在 @Bean
上:
构造方法绑定 - @ConstructorBinding
构造方法绑定(Constructor binding)
构造方法绑定只能用 @EnableConfigurationProperties
,不能使用 @Component
的方式
宽松绑定 (Relaxed Binding) - 概念
配置文件中的属性名格式比较宽松,有多种风格:
- Kebab 格式(烤肉串格式),推荐这种写法:
acme.my-project.person.first-name
- 驼峰写法:
acme.myProject.person.firstName
- 下划线写法:
acme.my_project.person.first_name
- 变量名大写:使用系统环境变量时建议使用
ACME_MYPROJECT_PERSON_FIRSTNAME
拆分配置文件
文件名 application-*
的后半部分默认就是文档名
# application.yml
spring:profiles:active: [cat, student]person:age: 11name: Person-{person.age}
# application-cat.yml
cat:age: 22name: Cat-${cat.age}
# application-student.yml
student:age: 33name: Student-${student.age}
在 application.yml 中引入了 application-cat.yml 和 application-student.yml,相当于如下效果:
# application.yml
person:age: 11name: Person-{person.age}cat:age: 22name: Cat-${cat.age}student:age: 33name: Student-${student.age}
banner
参考:官网文档
Banner 生成:http://patorjk.com/software/taag
【Java从0到架构师】SpringBoot - 入门_配置文件_YAML相关推荐
- 【Java从0到架构师】RocketMQ 使用 - 集成 SpringBoot
RocketMQ 消息中间件 集成 SpringBoot 入门案例 生产消息类型 - 同步.异步.一次性 消费模式 - 集群.广播 延时消息 设置消息标签 设置消息的 Key 自定义属性设置 消息过滤 ...
- 【Java从0到架构师】SpringBoot - SpringMVC
SpringBoot - SpringMVC SpringMVC 的配置 文件上传功能 文件下载 - 以附件形式下载 静态资源访问 静态资源访问 - 映射 静态资源访问 - webjars Java ...
- 【Java从0到架构师】交错的日志系统、SpringBoot 集成日志框架
交错的日志系统.SpringBoot 集成日志框架 交错复杂的日志系统① - 多个项目实现 SLF4J 门面 交错复杂的日志系统② - 统一底层实现为 Logback 交错复杂的日志系统③ - 统一底 ...
- 【Java从0到架构师】SpringBoot - MyBatis
SpringBoot - MyBatis 集成 MyBatis 引入依赖 数据源配置 - 源码 MyBatis 配置 - 源码 扫描 Dao MyBatis 主配置 - XML.注解.applicat ...
- 【Java从0到架构师】SpringBoot - 页面模版_Thymeleaf
SpringBoot - 页面模版 集成 JSP 集成 Thymeleaf Thymeleaf 基本语法 注释 - 3 种 字面量 - ${} [[ ${} ]] 局部变量 - th:with.三目运 ...
- 【Java从0到架构师】git 入门和基本应用
git 核心原理剖析和基本应用 Git 入门 初始化版本库 把文件添加到版本库 查看日志 版本号问题 Git - 时间穿梭机 版本仓库的原理 查看版本库状态 - git status 查看差异 - g ...
- 【Java从0到架构师】项目实战 - 会话管理、EhCache、JWT、权限管理 Shiro、打包部署
项目实战 - 权限管理 会话管理 客户端身份认证 - 基于 Cookie.Session 客户端身份验证 - 基于 token EhCache - 简单的缓存框架 JWT - 基于 JSON 的 to ...
- 【Java从0到架构师(1),Java中高级面试题总结(全面)
JSP 九大内置对象 MySQL 基础 + 多表查询 [Java从0到架构师]MySQL 基础 MySQL MySQL 的使用步骤 数据库的内部存储细节 GUI 工具 SQL 语句 DDL 语句 DD ...
- 【Java从0到架构师】SpringCloud - Hystrix、Zuul
SpringCloud 基本概念 熔断和降级 服务雪崩效应 服务熔断与降级 - Hystrix SpringBoot 集成 Hystrix 熔断降级服务异常报警通知 重点属性 - 熔断隔离策略.超时时 ...
最新文章
- 10分钟完成一个业务流程的发布
- 字符串相似度匹配算法python_算法字符串相似度得分/哈希
- 机器学习系统:设计与实现 计算图
- java jar 配置文件路径_java jar 配置文件的相对路径问题
- HTMLParser使用
- android透明状态栏刘海屏,Android 修改dialog状态栏颜色兼容刘海屏
- lc 297. Serialize and Deserialize Binary Tree
- 数据库lib7第2, 3题(创建索引和触发器)
- linux系统shell脚本编程,Linux系统shell脚本编程(一)
- 轴承过盈量程序matlab,轴承matlab处置程序.doc
- react报错Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
- 阿里云盘下载与公测预约(最新)
- 最小公共子串——力扣
- cad图纸比对lisp_CompareDWG|CAD图纸比较软件(CompareDWG)下载v2018 官方版 - 欧普软件下载...
- 嵌入式系统的知识体系
- 如何在Outlook中单击邮件后立即将其标记为已读
- 怀院计科院1901班NICE组限时答题程序
- 834. 树中距离之和
- webrtc部分手机黑屏的原因排查
- 打鸟Style 经典游戏再现