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要加上cj
    driver-class-name: com.mysql.cj.jdbc.Driver
    name: root
    password: flzxsqc
    url: 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要加上cj
    driver-class-name: com.mysql.cj.jdbc.Driver
    name: root
    password: flzxsqc
    url: jdbc:mysql://127.0.0.1:3306/my_db?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=GMT%2B8

# 用户自定义配置
config:
  customer:
    # 字符串【只有双引号才会被解析】
    str1: hello world
    str2: hello \n world
    str3: 'hello \n world'
    str4: "hello \n world"
    # 整数
    num1: 3
    # 浮点数
    float1: 3.1415926
    # Null【~代表null】
    null1: ~

# 对象
student:
  id: 1
  name: "张三"
  age: 23

student2: { id: 2, name: "李四", age: 24 }

# 集合
mylist:
  dbtypes:
    - mysql
    - oracle
    - sqlserver

mylist2: { 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: 1
  name: "张三"
  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;

    @Autowired
    public 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;

    @Autowired
    public 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.mfbz.cn/a/376940.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

IS-IS P2P网路类型 地址不在同一子网建立邻居关系

拓扑图 由于IS-IS是直接运行在数据链路层上的协议&#xff0c;并且最早设计是给CLNP使用的&#xff0c;IS-IS邻居关系的形成与IP地址无关。但在实际的实现中&#xff0c;由于只在IP上运行IS-IS&#xff0c;所以是要检查对方的IP地址的。如果接口配置了从IP&#xff0c;那么只要…

年假作业6

#include<stdio.h> #include<string.h> int main(int argc, const char *argv[]) { int data0; int a,b; printf("请输入数据data\n"); scanf("%d",&data); adata|1<<5; ba&~(1<<3); printf(&quo…

基于STM32平台的嵌入式AI音频开发

加我微信hezkz17&#xff0c;可申请加入 嵌入式人工智能开发交流答疑群。 1 stm32芯片AI开发流程 其中模型也可以选择tensorflow &#xff0c;pytorch 2 FP-AI-SENSING1 SDK开发包介绍 3 声音场景分类项目数据集选择 (1)自己采集数据打标签 (2) 使用专用数据集 4 完整参考

收到微信发的年终奖。。。

大家好&#xff0c;我是小悟 还剩一天就过除夕了&#xff0c;很多单位都已经放假了&#xff0c;街上的人越来越少&#xff0c;门店关着的很多&#xff0c;说明大家都陆陆续续回自己的家乡过年了。 或许你还在搬砖&#xff0c;坚守节前最后一波工作&#xff0c;或许你正在回家的…

数据结构-->线性表-->顺序表

对我个人来说&#xff0c;C语言基础相关的知识基本学完了&#xff0c;随后就该学数据结构了&#xff0c;希望以后自己复习能够用上今天自己写的哈哈。 如果你不理解什么是物理结构和逻辑结构&#xff0c;这里附上一个链接&#xff1a;逻辑结构和物理结构&#xff1a;逻辑结构与…

从Unity到Three.js(安装启动)

发现在3D数字孪生或模拟仿真方向&#xff0c;越来越多的公司倾向使用Web端程序&#xff0c;目前一直都是使用的Unity进行的Web程序开发&#xff0c;但是存在不少问题&#xff0c;比如内存释放、shader差异化、UI控件不支持复制或输入中文等。虽然大多数问题都可以找到解决方案&…

MySQL:从基础到实践(简单操作实例)

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 下载前言一、MySQL是什么&#xff1f;二、使用步骤1.引入库2.读入数据 提交事务查询数据获取查询结果总结 下载 点击下载提取码888999 前言 在现代信息技术的世界…

RabbitMQ-6.延迟消息

延迟消息 6.延迟消息6.1.死信交换机和延迟消息6.1.1.死信交换机6.1.2.延迟消息6.1.3.总结 6.2.DelayExchange插件6.2.1.下载6.2.2.安装6.2.3.声明延迟交换机6.2.4.发送延迟消息 6.延迟消息 在电商的支付业务中&#xff0c;对于一些库存有限的商品&#xff0c;为了更好的用户体…

SpringBoot:web开发

web开发demo&#xff1a;点击查看 LearnSpringBoot05Web 点击查看更多的SpringBoot教程 技术摘要 webjarsBootstrap模板引擎thymeleaf嵌入式Servlet容器注册web三大组件 一、webjars webjars官网 简介 简介翻译 WebJars 是打包到 JAR&#xff08;Java Archive&#xff09;…

2024.02 国内认知大模型汇总

概述 大模型&#xff0c;又称为大规模机器学习模型&#xff0c;是一种基于大数据的人工智能技术。它通过深度学习和机器学习的方法&#xff0c;对大量数据进行训练&#xff0c;以实现对复杂问题的高效解决。大模型技术在语音识别、图像识别、自然语言处理等领域有着广泛的应用…

常用对象和常用成员函数

常量对象与常量成员函数来防止修改对象&#xff0c;实现最低权限原则。 在Obj被定义为常量对象的情况下&#xff0c;下面这条语句是错误的。 错误的原因是常量对象一旦初始化后&#xff0c;其值就再也不能改变。因此&#xff0c;不能通过常量对象调用普通成员函数&#xff0c;因…

ArcGIS学习(五)坐标系-2

3.不同基准面坐标系之间的转换 在上一关中,我们学习了ArcGIS中的投影(投影栅格)工具,并以"WGS1984地理坐标系与WGS1984的UTM投影坐标系的转换”为例进行讲解。 "WGS1984地理坐标系与WGS1984的UTM投影坐标系的转换”代表的是同一个基准面下的两个坐标的转换。 …

【力扣】查找总价格为目标值的两个商品,双指针法

查找总价格为目标值的两个商品原题地址 方法一&#xff1a;双指针 这道题和力扣第一题“两数之和”非常像&#xff0c;区别是这道题已经把数组排好序了&#xff0c;所以不考虑暴力枚举和哈希集合的方法&#xff0c;而是利用单调性&#xff0c;使用双指针求解。 考虑数组pric…

洛希极限

L1-3 洛希极限 分数 10 作者 陈越 单位 浙江大学 科幻电影《流浪地球》中一个重要的情节是地球距离木星太近时&#xff0c;大气开始被木星吸走&#xff0c;而随着不断接近地木“…

【数据结构】链表OJ面试题2《分割小于x并排序链表、回文结构、相交链表》+解析

1.前言 前五题在这http://t.csdnimg.cn/UeggB 休息一天&#xff0c;今天继续刷题&#xff01; 2.OJ题目训练 1. 编写代码&#xff0c;以给定值x为基准将链表分割成两部分&#xff0c;所有小于x的结点排在大于或等于x的结点之前 。链表分割_牛客题霸_牛客网 思路 既然涉及…

春节回家坐飞机后助听器就不好用了?如何过安检、拖运?

春节即将来临&#xff0c;很多人都要乘坐飞机回家或者出游。如果你是一位助听器使用者&#xff0c;你可能会有一些疑问&#xff1a;坐飞机能戴助听器吗&#xff1f;助听器会不会受到安检设备的影响&#xff1f;直接将助听器放在传送带上可以吗&#xff1f;……别担心&#xff0…

Harbor介绍、整体架构和安装

Harbor介绍、整体架构和安装 文章目录 Harbor介绍、整体架构和安装1.Harbor介绍2.Harbor 整体架构3.安装Harbor3.1 主机初始化3.1.1 设置ip地址3.1.2 配置镜像源3.1.3 关闭防火墙3.1.4 禁用SELinux3.1.5 禁用swap3.1.6 设置时区 3.2 安装docker3.3 安装docker compose3.4 下载H…

【JS逆向一】逆向某站的 加密参数算法--仅供学习参考

逆向日期&#xff1a;2024.02.06 使用工具&#xff1a;Node.js 文章全程已做去敏处理&#xff01;&#xff01;&#xff01; 【需要做的可联系我】 可使用AES进行解密处理&#xff08;直接解密即可&#xff09;&#xff1a;在线AES加解密工具 1、打开某某网站(请使用文章开头的…

记录 | linux下切换python版本

查看系统中存在的 python 版本 ls /usr/bin/python* 查看系统默认的 python 版本 python --version
最新文章