【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://xiahunao.cn/news/3246189.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

【STM32嵌入式系统设计与开发---拓展】——1_9_1上拉输入和下拉输入

在使用GPIO引脚时&#xff0c;上拉输入和下拉输入的选择取决于外部电路的特性和应用需求。以下是它们各自的应用场景&#xff1a; 1、上拉输入&#xff08;Pull-up Input&#xff09; 用途: 当默认状态需要为高电平时。 避免引脚悬空&#xff08;floating&#xff09;导致的…

逆向学习思路链接分享

学好逆向先学C 然后我们需要学习好 编码问题CTF常见编码及加解密&#xff08;超全&#xff09; - ruoli-s - 博客园 (cnblogs.com) 并且规划好学习路线 CTF逆向Reverse入门学习路线&#xff08;面向小白&#xff09;_逆向reverse 思路-CSDN博客 并且安好反编译的环境 x64d…

微信保存的图片很模糊,用这个软件,秒变高清图!

我们有时从微信下载下来的图片就是很模糊&#xff0c;重新加载也一样&#xff0c;不知道什么原因。那么有什么好的解决图片模糊的办法吗&#xff1f; 微信保存的图片很模糊&#xff0c;用这个软件&#xff0c;秒变高清图&#xff01; 或者是写一个东西&#xff0c;需要配图&am…

在项目服务器部署git 并实现自动提交

以下场景适合在服务器当中使用git 方便提交代码&#xff0c;同时不需要外部的git仓库&#xff08;码云gitee或者github作为管理平台&#xff09;。依靠服务器本身ssh 连接协议做为git提交的地址&#xff0c;同时利用钩子自动同步项目代码 首先下载git sudo apt update sudo a…

什么是 EDI 电子数据交换? EDI 有哪些优势?EDI 解决方案 以及行业应用

什么是EDI电子数据交换&#xff1f; EDl电子数据交换(Electronic Data |nterchange)是指按照同一规定的一套通用标准格式&#xff0c;将标准的数据信息通过通信网络传输&#xff0c;在贸易伙伴的电子计算机系统之间进行数据交换和自动处理。简单来说&#xff0c;EDI是将贸易、…

2024Datawhale AI夏令营---Inclusion・The Global Multimedia Deepfake Detection--学习笔记

赛题背景&#xff1a; 其实总结起来就是一句话&#xff0c;这个项目是基于目前的深度伪装技术&#xff0c;就是通过大量人脸的原数据集进行模型训练之后&#xff0c;能够生成伪造的人脸视频。这项目就是教我们如何去实现这个DeepFake技术。 Task1:了解Deepfake和跑通baseline …

C语言 | Leetcode C语言题解之第237题删除链表中的节点

题目&#xff1a; 题解&#xff1a; /*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/void deleteNode(struct ListNode* node) {struct ListNode * p node->next;int temp;temp node->val;node->val…

C++从入门到起飞之——inline/nullptr关键字全方位剖析!

个人主页&#xff1a;秋风起&#xff0c;再归来~ C从入门到起飞 个人格言&#xff1a;悟已往之不谏&#xff0c;知来者犹可追 克心守己&#xff0c;律己则安&#xff01; 目录 1、inline 2、nullptr 3.完结散花 1、inline • ⽤inline修饰的函数叫…

苹果手机的微信过期文件怎么恢复?3个小窍门,让你快速找回

在微信APP里&#xff0c;发送过的文件只能储存7天&#xff0c;7天之后就会自动清除&#xff0c;导致无法打开。那么&#xff0c;微信过期文件怎么恢复呢&#xff1f;别担心&#xff0c;今天我们就来分享3个实用的小窍门&#xff0c;帮助你轻松恢复苹果手机上过期的微信文件。赶…

React Native 自定义 Hook 获取组件位置和大小

在 React Native 中自定义 Hook useLayout 获取 View、Pressable 等组件的位置和大小的信息 import {useState, useCallback} from react import {LayoutChangeEvent, LayoutRectangle} from react-nativeexport function useLayout() {const [layout, setLayout] useState&l…

springcolud学习03Eureka

Eureka 模块 来实现服务治理 服务治理就是提供了微服务架构中各微服务实例的快速上线或下线且保持各服务能正常通信的能力的方案总称 建立eureka模型 导入依赖 <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XM…

Linux 上 TTY 的起源

注&#xff1a;机翻&#xff0c;未校对。 What is a TTY on Linux? (and How to Use the tty Command) What does the tty command do? It prints the name of the terminal you’re using. TTY stands for “teletypewriter.” What’s the story behind the name of the co…

浅谈Visual Studio 2022

Visual Studio 2022&#xff08;VS2022&#xff09;提供了众多强大的功能和改进&#xff0c;旨在提高开发者的效率和体验。以下是一些关键功能的概述&#xff1a;12 64位支持&#xff1a;VS2022的64位版本不再受内存限制困扰&#xff0c;主devenv.exe进程不再局限于4GB&#xf…

SQL常用数据过滤---IN操作符

在SQL中&#xff0c;IN操作符常用于过滤数据&#xff0c;允许在WHERE子句中指定多个可能的值。如果列中的值匹配IN操作符后面括号中的任何一个值&#xff0c;那么该行就会被选中。 以下是使用IN操作符的基本语法&#xff1a; SELECT column1, column2, ... FROM table_name WH…

用Vue3和WebCola实现3D图的在线展示

本文由ScriptEcho平台提供技术支持 项目地址&#xff1a;传送门 基于Cola.js的网络图绘制 应用场景 Cola.js是一个JavaScript库&#xff0c;用于绘制交互式网络图。它广泛应用于社交网络、知识图谱、生物网络等领域&#xff0c;帮助用户可视化和探索复杂的数据关系。 基本…

c语言唯一一个三目运算符

条件表达式由两个符号&#xff08;&#xff1f;和&#xff1a;&#xff09;组成&#xff0c;必须一起使用。要求有三个操作对象&#xff0c;称为三目运算符。 一般形式为 表达式1&#xff1f;表达式2&#xff1a;表达式3 理解如下&#xff1a; a>b?(maxa):(maxb); //相当…

用AI生成Springboot单元测试代码太香了

你好&#xff0c;我是柳岸花开。 在当今软件开发过程中&#xff0c;单元测试已经成为保证代码质量的重要环节。然而&#xff0c;编写单元测试代码却常常让开发者头疼。幸运的是&#xff0c;随着AI技术的发展&#xff0c;我们可以利用AI工具来自动生成单元测试代码&#xff0c;极…

JS+CSS特效:HTML+JS+CSS 实现精致的带二级菜单的头部菜单

本篇&#xff0c;我们来演示一个二级菜单是怎么做出来的。 案例效果图 因为本次内容主要目标是实现顶部的导航菜单&#xff0c;所以我们不关心其他内容。 第一步&#xff1a;清除浏览器默认样式 & 添加基本样式 *{ margin: 0px; padding: 0px; box-sizing: border-box; …

万界星空科技电线电缆行业MES系统核心功能

在日新月异的科技浪潮中&#xff0c;电线电缆行业作为国民经济的重要支柱&#xff0c;正面临着前所未有的挑战与机遇。如何在激烈的市场竞争中脱颖而出&#xff0c;实现生产效率与产品质量的双重飞跃&#xff0c;成为了每一家线缆企业亟需解决的课题。万界星空科技&#xff0c;…

电池放电倍率

电池放电倍率是指电池在单位时间内放电的速率与其额定容量之比 &#xff0c;放电倍率越大&#xff0c;表示电池能够在较短的时间内释放更多的电能。一般来说&#xff0c;电池的放电倍率会影响其使用时的性能和寿命。 电池的放电倍率主要取决于其设计和制造工艺。一般来说&#…