【SpringBoot配置文件application.yaml】笔记

详细内容见官方文档Common Application Properties
在这里插入图片描述

使用application.yaml进行简单配置

    • 第一步:创建WebDemo
    • 第二步:创建application.yaml配置文件
        • 注意:
    • 第三步:验证自己创建的yaml文件是否生效
        • 测试:
        • 思考:如果application.properties和application.yaml中都配置了端口号到底那个生效呢?
            • 结论
    • 第四步:使用application.yaml配置文件给实体类注入属性值
      • ① 创建实体类Student
      • ②在application.yaml中配置Student的属性值
        • 测试:
        • 思考:如果在application.properties中也配置student的属性,这两个配置文件那个生效?
          • 结论:

第一步:创建WebDemo

创建一个SpringBoot的WebDemo,创建WebDemo具体步骤—>如何创建一个SpringBoot的WebDemo】笔记方法二使用Spring Initializr创建一个Spring Boot项目
按照上述链接笔记中的步骤创建好WebDemo后测试正常运行后再进行下面的步骤,
此时项目的默认端口号应该为8080

第二步:创建application.yaml配置文件

操作步骤: 选中resources文件夹—>鼠标右键—>选择New—>file—>application.yaml
在这里插入图片描述
在这里插入图片描述

注意:

此时的项目中是有两个配置文件的,application.properties和application.yaml到底那个生效呢?都生效吗?只有其中一个生效?

第三步:验证自己创建的yaml文件是否生效

使用application.yaml配置项目的端口号.因为端口号默认是8080,现在我们通过yaml文件修改端口号来验证配置是否生效。
在application.yaml文件中添加配置
修改项目端口号

server:port: 8088

如下图所示,此时application.properties和application.yaml中的内容,application.properties中什么都没有,application.yaml中配置了端口号8088
在这里插入图片描述

测试:
  • 启动SpringBoot项目
  • 在浏览器访问127.0.0.1:8088/hello
    具体测试步骤见如何创建一个SpringBoot的WebDemo】笔记 方法二使用Spring Initializr创建一个Spring Boot项目 的测试部分
    经测试发现,浏览器访问127.0.0.1:8088/hello,成功得到了响应,说明使用yaml配置文件修改端口号配置成功,项目的端口号不再是默认的8080了
    在这里插入图片描述
思考:如果application.properties和application.yaml中都配置了端口号到底那个生效呢?

测试:两种配置文件都配置端口号,测试到底那个生效


application.properties

server.port=8099

application.yaml

server:port: 8088

在这里插入图片描述
两个配置文件我们都配置端口号后重启项目,浏览器分别访问127.0.0.1:8088/hello
在这里插入图片描述
127.0.0.1:8099/hello
在这里插入图片描述

结论

发现8099端口可以访问,application.properties优先级是高于application.yaml的

第四步:使用application.yaml配置文件给实体类注入属性值

具体步骤:

  • ① 创建实体类Student
  • ②在application.yaml中配置Student的属性值

① 创建实体类Student

在org.example.springweb_hello下创建文件夹pojo,在pojo中创建Student类和Pet类
Student类

package org.example.springweb_hello.pojo;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
//使用注解@ConfigurationProperties(prefix = "student"),与yaml文件中student的属性值进行绑定
@ConfigurationProperties(prefix = "student")
@Component//@Component将该类注入到IOC容器中
public class Student {private String userName;private Boolean flage;private Date birth;private Integer age;private Pet favoritePet;private String[] interests;private List<String> animal;private Map<String, Object> score;private Set<Double> salarys;private Map<String, List<Pet>> allPets;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public Boolean getFlage() {return flage;}public void setFlage(Boolean flage) {this.flage = flage;}public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Pet getFavoritePet() {return favoritePet;}public void setFavoritePet(Pet favoritePet) {this.favoritePet = favoritePet;}public String[] getInterests() {return interests;}public void setInterests(String[] interests) {this.interests = interests;}public List<String> getAnimal() {return animal;}public void setAnimal(List<String> animal) {this.animal = animal;}public Map<String, Object> getScore() {return score;}public void setScore(Map<String, Object> score) {this.score = score;}public Set<Double> getSalarys() {return salarys;}public void setSalarys(Set<Double> salarys) {this.salarys = salarys;}public Map<String, List<Pet>> getAllPets() {return allPets;}public void setAllPets(Map<String, List<Pet>> allPets) {this.allPets = allPets;}
}

Pet类

package org.example.springweb_hello.pojo;import org.springframework.stereotype.Component;@Component//@Component将该类注入到IOC容器中
public class Pet {private String name;private Double weight;public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getWeight() {return weight;}public void setWeight(Double weight) {this.weight = weight;}
}

②在application.yaml中配置Student的属性值

application.yaml中的配置如下所示

# 用yaml表示student对象
student:userName: jiekkiflage: falsebirth: 2019/12/12 20:12:33age: 18
#  favoritePet对象类型,可以用这种形式
#  favoritePet:
#    name: tomcat
#    weight: 23.4
#  favoritePet对象类型,也能用这种形式favoritePet: { name: hotdog,weight: 38.9}
#    数组interests可以用这种形式
#  interests:
#    - 篮球
#    - 游泳
#    - 唱歌
#数组interests也能用这种形式 interests: [ 篮球,跳舞 ]interests: [ 篮球,跳舞 ]
#  集合animal可以用这种形式
#  animal:
#    - jerry
#    - mario
#    - tom
#  集合animal也能用这种形式animal: [jerry,mario,tom,cxk]animal: [jerry,mario,tom,cxk]score:english:first: 30second: 40third: 50math: [ 131,140,148 ]chinese: { first: 128,second: 136 }salarys: [ 3999,4999.98,5999.99 ]allPets:sick:- { name: tom }- { name: jerry,weight: 47 }health: [ { name: mario,weight: 47 } ]server:port: 8088
测试:

测试目标:浏览器向127.0.0.1:8088/hello发送请求,将Student对象作为响应返回
测试步骤:
测试结果:浏览器拿到了student数据

  • 编写HelloController
package org.example.springweb_hello.controller;
import org.example.springweb_hello.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {@Autowired//将IOC容器中的student对象注入进来Student student;@GetMapping("/hello")public Student hello(){return student;//返回student对象}
}
  • 浏览器发送请求
    在这里插入图片描述
思考:如果在application.properties中也配置student的属性,这两个配置文件那个生效?

在这里插入图片描述

application.properties

student.userName=lihua
student.age=39
student.flage=true

重启项目—>浏览器重启发送请求127.0.0.1:8088/hello
在这里插入图片描述

结论:

发现application.properties优先级是高于application.yaml的,application.properties中的配置是优先生效的,application.properties中存在的配置先生效,如果application.properties中没有就去找application.yaml中的配置

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

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

相关文章

算法刷题笔记 八数码(C++实现)

文章目录 题目描述基本思路实现代码 题目描述 在一个33的网格中&#xff0c;1∼8这8个数字和一个x恰好不重不漏地分布在这33的网格中。例如&#xff1a; 1 2 3 x 4 6 7 5 8 在游戏过程中&#xff0c;可以把x与其上、下、左、右四个方向之一的数字交换&#xff08;如果存在&…

MT7628指定分区备份固件

为了避免升级过程突然断电&#xff0c;或者其他不良操作导致的路由器“变砖”。在MT7628使用过程中&#xff0c;我们可以对固件进行备份。 MT7628原厂SDK有关于双备份的选项&#xff0c;选择对应选项后&#xff0c;可对固件进行备份。下面以SKYLAB的SKW92A模组为例进行测试说明…

【专项刷题】— 快排

1、颜色分类 - 力扣&#xff08;LeetCode&#xff09; 思路&#xff1a; 创建三个指针&#xff0c;然后把数组分为三个区域遍历代码&#xff1a; class Solution {public void swap(int[] nums, int i, int j){int t nums[i];nums[i] nums[j];nums[j] t;}public void sortCo…

百度网盘Android一二面凉经(2024)

百度网盘Android一二面凉经(2024) 笔者作为一名双非二本毕业7年老Android, 最近面试了不少公司, 目前已告一段落, 整理一下各家的面试问题, 打算陆续发布出来, 供有缘人参考。今天给大家带来的是《百度网盘Android一二面凉经(2024)》。 面试职位: 网盘主端研发组_Android高级研…

细说MCU用定时器控制单路DAC模块设计和输出锯齿波的实现方法

目录 一、参考工程 二、仅提供不同的配置 1、用定时器控制DAC输出 2、配置定时器参数 三、代码修改 四、 运行并观察显示效果 一、参考工程 本工程依赖作者的文章&#xff1a;细说MCU用单路DAC模块设计和输出锯齿波的实现方法-CSDN博客 https://wenchm.blog.csdn.net/ar…

Python 如何对上万、百万、亿级数据去重?

大家好&#xff01;我是爱摸鱼的小鸿&#xff0c;关注我&#xff0c;收看每期的编程干货。 今天我们要一起探索一个让数据工程师、数据科学家和开发者们都头疼的问题&#xff1a;如何对海量数据进行去重。随着数据量的不断增长&#xff0c;我们在处理数据时&#xff0c;去重操作…

要么利用规则,要么打破规则

在这个充满规则和标准的世界里&#xff0c;我们常常被告知要如何生活、如何成功。但事实上&#xff0c;这些规则和标准往往限制了我们的潜力和创造力。本文将探讨如何不被外界规则所束缚&#xff0c;活出自己的风采。 规则的双刃剑 规则和标准可以为社会带来秩序&#xff0c;…

JavaScript进阶之构造函数数据常用函数

目录 一、深入对象1.1 创建对象的三种方式1.2 构造函数1.3 实例成员&静态成员 二、内置构造函数2.1 Object2.2 Array2.3 String常见实例方法2.4 Number 一、深入对象 1.1 创建对象的三种方式 利用对象字面量创建对象利用new object创建对象 const obj new Object({})利用…

SAP 如何修改统驭科目类型

在SAP中&#xff0c;科目设置错了统驭科目类型并且记账了要如何修改&#xff1f; 例如&#xff1a;前期应收账款对应的统驭科目类型前期设置成了供应商&#xff0c;并且供应商用该科目过来账&#xff0c;现在需要调整&#xff0c;想要将供应商调整到客户&#xff0c;科目为当前…

Java | Leetcode Java题解之第268题丢失的数字

题目&#xff1a; 题解&#xff1a; class Solution {public int missingNumber(int[] nums) {int n nums.length;int total n * (n 1) / 2;int arrSum 0;for (int i 0; i < n; i) {arrSum nums[i];}return total - arrSum;} }

ros2--QOS--通信质量

在ros2通信编程中&#xff0c;总有一个和qos相关的参数&#xff1a; publisher: template<typename MessageT, typename AllocatorT, typename PublisherT> std::shared_ptr<PublisherT> Node::create_publisher(const std::string & topic_name,const rclcp…

Linux安装Redis5.0镜像、Mysql8.0镜像

docker 安装Redis &#xff08;全网最详细&#xff1a;附带配置文件&#xff09;_docker pull redis-CSDN博客 Docker实用-安装Mysql8 - 简书 改动&#xff1a; 指定&#xff1a;docker pull mysql:8.0 docker run -d -p 3306:3306 \ --restartalways \ --privilegedtrue \…

088、Python 读取Excel文件及一些操作(使用xlwtxlrd库)

要读取Excel文件&#xff0c;我们需要使用第三方库。 xlrd库是一个常用的读取Excel的第三方库&#xff0c;它同时支持.xls和.xlsx&#xff0c;不过xlrd从版本2.0.0开始不再支持.xlsx的读取&#xff0c;需要单独使用openpyxl。 要使用第三方库&#xff0c;首选需安装&#xff…

学习系列一:YOLO系列目标检测框架之间介绍及对比

YOLO系列目标检测框架之间介绍及对比 华为HCIP AI高级工程师证书&#xff0c; 华为HCIA AI证书&#xff0c;目前从事视觉算法工作 文章目录 YOLO系列目标检测框架之间介绍及对比前言一、YOLOv1二、YOLOv2三、YOLOv3四、YOLOv4五、YOLOv5及后续算法 前言 YOLO系列算法 YOLO 创…

树与二叉树学习笔记

树与二叉树 计算机中的树树的概念树的类型 什么是二叉树二叉树&#xff1a;定义与特点二叉树&#xff1a;前序、中序、后序遍历二叉树&#xff1a;深度、广度优先遍历二叉树&#xff1a;线索化二叉树&#xff1a;序列化与反序列化 haffman树平均编码长度构建haffman树haffman树…

数学建模算法汇总(全网最全,含matlab案例代码)

数学建模常用的算法分类 全国大学生数学建模竞赛中&#xff0c;常见的算法模型有以下30种&#xff1a; 最小二乘法数值分析方法图论算法线性规划整数规划动态规划贪心算法分支定界法蒙特卡洛方法随机游走算法遗传算法粒子群算法神经网络算法人工智能算法模糊数学时间序列分析马…

大模型应用—大模型赋能网络爬虫

大模型赋能网络爬虫 简单来说,网页抓取就是从网站抓取数据和内容,然后将这些数据保存为XML、Excel或SQL格式。除了用于生成潜在客户、监控竞争对手和市场研究外,网页抓取工具还可以用于自动化你的数据收集过程。 借助AI网页抓取工具,可以解决手动或纯基于代码的抓取工具的…

shell脚本语言的入门

&#x1f4d1;打牌 &#xff1a; da pai ge的个人主页 &#x1f324;️个人专栏 &#xff1a; da pai ge的博客专栏 ☁️宝剑锋从磨砺出&#xff0c;梅花香自苦寒来 ☁️运维工程师的职责&#xff1a;监…

pytorch深度学习框架基本介绍

目录 1. PyTorch简介1.1 什么是PyTorch1.2 PyTorch的特点 2. 安装与配置2.1 安装PyTorch2.2 配置CUDA环境 3. 基础概念3.1 张量(Tensor)3.1.1 创建张量3.1.2 张量的类型转换 3.2 自动微分(Autograd) 4. 构建神经网络4.1 定义网络结构4.2 使用nn.Module 5. 数据加载与处理5.1 使…

腾讯技术创作特训营 -- SUPERWINNIE -- AI重塑社交内容

目录 1 什么是AI社交内容 2 案例拆解 3 用LLM做爆文选题 4 用LLM出爆文脚本提示词 1 什么是AI社交内容 任何一个因素被AI取代都是AI社交内容 2 案例拆解 数字人 资讯素材 录屏产品的素材&#xff08;小红书测试AI产品&#xff09; 脚本 素材 剪辑 3 用LLM做爆文选题 &…