SpringBoot配置文总结

官网配置手册

官网:https://spring.io/
选择SpringBoot
在这里插入图片描述

选择LEARN
在这里插入图片描述

选择 Application Properties
在这里插入图片描述

配置MySQL数据库连接

针对Maven而言,会搜索出两个MySQL的连接驱动。
在这里插入图片描述
com.mysql » mysql-connector-j
比较新,是在mysql » mysql-connector-java基础上进行二次开发和维护
在这里插入图片描述
mysql » mysql-connector-java也说明了转移到了com.mysql » mysql-connector-j,推荐使用com.mysql » mysql-connector-j【如果是老项目,则应该选择mysql » mysql-connector-java】
在这里插入图片描述

spring:datasource:#    MySQL8.x要加上cjdriver-class-name: com.mysql.cj.jdbc.Drivername: rootpassword: flzxsqcurl: jdbc:mysql://127.0.0.1:3306/my_db?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=GMT%2B8

SpringBoot这里配置的是UTF-8,但是会被默认的数据库连接池HikariCP解析时映射到MySQL的utf8mb4字符集上。
HikariCP使用mysql-connector-j作为数据库连接驱动,而mysql-connector-j对于字符集utf-8的解释会映射为utf8mb4格式,进而更好的支持Unicode特殊字符比如 Emoji 表情等

读取配置文件信息

properties格式

properties配置文件

  1. 同样的代码需要多次写,会不方便

格式:key.value

# 应用服务 WEB 访问端口
server.port=8080
# MySQL数据库连接
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/my_db&?useUnicode=true&characterEncoding=UTF-8&userSSL=true&serverTimezone=GMT%2B8
spring.datasource.name=root
spring.datasource.password=flzxsqc
# MySQL8.x加cj
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# 用户自定义配置
config.customer.str1=hello

yaml格式

yaml格式配置文件

  1. 类似于JSON格式,可读性高,写法简单易理解
  2. 支持的数据类型众多【数组、散列表】
  3. 支持的编程语言更多【PHP、Python、JavaScript】

格式:key: value
注意: key 和 value 之间使用英文冒号加空格形式组成,其中空格不能省略

# 应用服务 WEB 访问端口
server:port: 8080# MySQL数据库连接# 用户自定义配置
spring:datasource:#    MySQL8.x要加上cjdriver-class-name: com.mysql.cj.jdbc.Drivername: rootpassword: flzxsqcurl: jdbc:mysql://127.0.0.1:3306/my_db?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=GMT%2B8# 用户自定义配置
config:customer:# 字符串【只有双引号才会被解析】str1: hello worldstr2: hello \n worldstr3: 'hello \n world'str4: "hello \n world"# 整数num1: 3# 浮点数float1: 3.1415926# Null【~代表null】null1: ~# 对象
student:id: 1name: "张三"age: 23student2: { id: 2, name: "李四", age: 24 }# 集合
mylist:dbtypes:- mysql- oracle- sqlservermylist2: { dbtypes: [ mysql2, oracle2, sqlserver2 ] }

yml配置文件中如果使用双引号修饰了字符,那么其中的特殊字符就会生成对应的效果比如 \n 换行符

读取基础数据

把properties文件注释掉,只读取yaml文件数据

采用@Value注解读取配置文件中基础数据类型
前端页面效果【被转为了JSON字符串】
Java代码如下

package app.controller;import app.model.MyList;
import app.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.beans.factory.annotation.Value;import java.util.ArrayList;
import java.util.List;@RestController
public class TestController {// 读取用户自定义配置信息@Value("${config.customer.str1}")private String config_customer_str1;@Value("${config.customer.str2}")private String config_customer_str2;@Value("${config.customer.str3}")private String config_customer_str3;@Value("${config.customer.str4}")private String config_customer_str4;@Value("${config.customer.num1}")private Integer config_customer_num1;@Value("${config.customer.float1}")private float config_customer_float1;@Value("${config.customer.null1}")private Object config_customer_null1;// 读取系统的配置项@Value("${server.port}")private String server_port;@GetMapping("/config")public List<Object> readConfig() {List<Object> config = new ArrayList<>();config.add(config_customer_str1);config.add(config_customer_str2);config.add(config_customer_str3);config.add(config_customer_str4);config.add(config_customer_num1);config.add(config_customer_float1);config.add(config_customer_null1);config.add(server_port);System.out.println(config);return config;}
}

在这里插入图片描述
控制台输出效果
在这里插入图片描述

读取对象

@ConfigurationProperties 对于配置文件中的赋值依赖 getter 和 setter 方法,缺少之后就会无法启动项目

# 对象
student:id: 1name: "张三"age: 23
# 类似于js的行内写法也可以读取到
student2: {id: 2, name: "李四", age: 24}

Java代码如下
构造对象

package app.model;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "student") // 将配置文件中 student 配置赋值给当前对象
public class Student {private Integer id;private String name;private Integer age;
}

读取对象

package app.controller;import app.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;import java.util.ArrayList;
import java.util.List;@RestController
public class TestController {// 读取对象private final Student student;@Autowiredpublic TestController(Student student) {this.student = student;}@GetMapping("/config")public List<Object> readConfig() {List<Object> config = new ArrayList<>();config.add(student);System.out.println(config);return config;}
}

在这里插入图片描述

读取集合

构造集合

package app.model;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.List;@Data
@Component
@ConfigurationProperties(prefix = "mylist2")
public class MyList {private List<String> dbtypes;
}

读取集合

package app.controller;import app.model.MyList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;import java.util.ArrayList;
import java.util.List;@RestController
public class TestController {// 读取集合private MyList myList;@Autowiredpublic TestController(MyList myList) {this.myList = myList;}@GetMapping("/config")public List<Object> readConfig() {List<Object> config = new ArrayList<>();config.add(myList);System.out.println(config);return config;}
}

在这里插入图片描述

多环境配置

多平台的配置文件明明也有格式要求,其中 application-xxx.yml是固定的,xxx 是可以随意修改的。一般来说:dev是开发环境;prod是生产环境;test是测试环境
在这里插入图片描述
在 application.yml 中管理配置文件
这样就会在项目启动时读取dev中配置

#配置文件管理
spring:profiles:active: dev

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/457566.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue3:24—组件通信方式

1、props 子组件也可以如下调用父组件的方法 2、自定义事件 &#xff08;emit&#xff09; 3、mitt&#xff08;任意组件的通讯&#xff09; 1. pubsub 2. $bus 3. mitt 接收数据的:提前绑定好事件(提前订阅消息)提供数据的:在合适的时候触发事件发布消息) 安装mitt npm i…

机试复习-3

前言&#xff1a;前面耽误太多时间&#xff0c;2月份是代码月&#xff0c;一定抓紧赶上&#xff0c;每天至少两道题 day1 2024.2.6 1.排序开启&#xff1a; 1.机试考试&#xff1a;排序应用考察 c的qsort c的sort 作用&#xff1a;对数组&#xff0c;vector排序&#…

项目经理怎么处理客户提出的不合理请求?

一、客户不合理请求的定义和特点 客户不合理请求是指客户在项目执行过程中提出的与项目需求、合同约定或者实际情况不符的要求&#xff0c;通常表现为追加要求、频繁的变更、过度的要求等。这些请求可能会导致项目范围膨胀、成本增加、工期延长、甚至影响项目进度和质量。客户…

前端基础复习(后端人员看前端知识)

企业级前端项目开发中&#xff0c;需要将前端开发所需要的工具、技术、流程、经验进行规范化和标准化&#xff0c;而不是零散的html、js、css文件堆叠在一起。 首先我们需配置前端的开发基础环境NodeJS&#xff0c;相当于后端人员java开发的JDK。然后搭建前端工程脚手架Vue-cl…

Golang GC 介绍

文章目录 0.前言1.发展史2.并发三色标记清除和混合写屏障2.1 三色标记2.2 并发标记问题2.3 屏障机制Dijkstra 插入写屏障Yuasa 删除写屏障混合写屏障 3.GC 过程4.GC 触发时机5.哪里记录了对象的三色状态&#xff1f;6.如何观察 GC&#xff1f;方式1&#xff1a;GODEBUGgctrace1…

LLaMA 模型中的Transformer架构变化

目录 1. 前置层归一化&#xff08;Pre-normalization&#xff09; 2. RMSNorm 归一化函数 3. SwiGLU 激活函数 4. 旋转位置嵌入&#xff08;RoPE&#xff09; 5. 注意力机制优化 6. Group Query Attention 7. 模型规模和训练超参数 8. 分布式模型训练 前置归一化与后置…

Linux的打包压缩与解压缩---tar、xz、zip、unzip

最近突然用到了许久不用的压缩解压缩命令&#xff0c;真的陌生&#xff0c; 哈哈&#xff0c;记录一下&#xff0c;后续就不用搜索了。 tar的打包 tar -cvf 压缩有的文件名称 需要压缩的文件或文件夹tar -cvf virtualbox.tar virtualbox/ tar -zcvf virtualbox.tar virtualbo…

【SpringBoot】application配置(5)

type-aliases-package: com.rabbiter.cm.domaintype-aliases-package: 这个配置用于指定mybatis的别名&#xff0c;别名是一个简化的方式&#xff0c;让你在Mapper xml 文件中引用java类型&#xff0c;而不需要使用使用完整的类名。例如&#xff0c;如果你在 com.rabbiter.cm.d…

MybatisPlus快速入门及常见设置

目录 一、快速入门 1.1 准备数据 1.2 创建SpringBoot工程 1.3 使用MP 1.4 获取Mapper进行测试 二、常用设置 2.1 设置表映射规则 2.1.1 单独设置 2.1.2 全局设置 2.2 设置主键生成策略 2.2.1 为什么会有雪花算法&#xff1f; 2.2.2 垂直分表 2.2.3 水平分表 2.…

Python进阶--下载想要的格言(基于格言网的Python爬虫程序)

注&#xff1a;由于上篇帖子&#xff08;Python进阶--爬取下载人生格言(基于格言网的Python3爬虫)-CSDN博客&#xff09;篇幅长度的限制&#xff0c;此篇帖子对上篇做一个拓展延伸。 目录 一、爬取格言网中想要的内容的url 1、找到想要的内容 2、抓包分析&#xff0c;找到想…

Verilog刷题笔记20

题目&#xff1a; Case statements in Verilog are nearly equivalent to a sequence of if-elseif-else that compares one expression to a list of others. Its syntax and functionality differs from the switch statement in C. 解题&#xff1a; module top_module ( …

STL算法(中)

常用排序算法 sort 功能描述&#xff1a; 对容器内元素进行排序 函数原型&#xff1a; sort(iterator beg, iterator end, _Pred) ; // 按值查找元素&#xff0c;找到返回指定位置迭代器&#xff0c;找不到返回结束迭代器位置 // beg 开始迭代器 // end 结束迭代器 …